2015-01-08 5 views
0

Я очень новичок в андроиде и нуждаюсь в небольшой помощи в решении этой задачи.GridView с изображением и текстом

У меня есть gridview witch pull изображения и текст из базы данных и их отображение. В настоящее время отображается как изображение, а справа от изображения - текст. Я хочу сделать текст под изображением. Можете ли вы помочь мне с этой задачей. Это gridview.xml

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.1" > 

    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:horizontalSpacing="5dp" 
     android:verticalSpacing="5dp" 
     android:numColumns="2" > 

    </GridView>    
</LinearLayout> 

И это, как я показываю изображение и текст в нем table_column.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/bckimageviw" 
> 
<ImageView 
    android:id="@+id/ColPhoto" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 

/> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <TextView android:id="@+id/ColName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="25dp" 
      android:text="Name" 
     /> 
</LinearLayout> 
</LinearLayout> 

И если потребность GetView()

 public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.table_column, null); 
     } 

     // ColPhoto 
     ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto); 
     imageView.getLayoutParams().height = 80; 
     imageView.getLayoutParams().width = 80; 
     imageView.setPadding(10, 10, 10, 10); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     try 
     { 
      imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap")); 
     } catch (Exception e) { 
      // When Error 
      imageView.setImageResource(android.R.drawable.ic_menu_report_image); 
     } 
     // ColName 
     TextView txtName = (TextView) convertView.findViewById(R.id.ColName); 
     txtName.setPadding(5, 0, 0, 0); 
     txtName.setText("" + MyArr.get(position).get("name").toString());  

     return convertView; 
    } 
+2

В главной LinearLayout в table_column.xml, установленная ориентация по вертикали;) – Tr4X

+1

Кстати, вам не нужен второй LinearLayout в вашем table_column.xml, вы можете поставить TextView в корне LinearLayout – Tr4X

ответ

1

и можно использовать относительное расположение и установить выравнивание textview к imageview.

enter image description here

попробовать это.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#ff0000" 
    android:gravity="center" > 

    <ImageView 
     android:id="@+id/ColPhoto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/ColName" 
     android:layout_alignRight="@+id/ColName" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/ColName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/ColPhoto" 
     android:gravity="center" 
     android:text="Name name" /> 

</RelativeLayout> 
+0

Но текст выше изображения не ниже –

+0

u означает под iamge? –

+0

Да, я пытаюсь поставить текст ниже изображения –

0

Попробуйте изменить ваша ориентация на «вертикальную».

EDIT:

Как, Tr4X сказал, что не нужно второй LinearLayout. Вы можете поставить оба в одном.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/bckimageviw" > 
<ImageView 
    android:id="@+id/ColPhoto" 
    android:layout_width="0dp" 
    android:layout_height="0dp"/> 

<TextView 
    android:id="@+id/ColName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" /> 
</LinearLayout> 
1

Я не знаю, зачем добавлять один элемент LinearLayout в другой LinearLayout. , если элемент сетки должен быть помещен в правильное положение, вместо этого я должен использовать RelativeLayout. `

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <ImageView 
     android:id="@+id/ColPhoto" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     /> 
    <TextView android:id="@+id/ColName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/ColPhoto" 
     android:text="Name" 
     /> 
</RelativeLayout> 

`

Смежные вопросы