2

我希望我的网格视图行放置在中心(水平)
我有两列宽度相同。
每个项目将被放置在相应列的中心。

我试图遵循其他答案。他们都没有工作。当简单的布局不能按预期工作时,真是令人沮丧……

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
  <GridView
      android:id="@+id/gridView_album"
      android:numColumns="2"

      android:stretchMode="columnmWidth"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:gravity="center"
      android:layout_gravity="center"
      >
  </GridView>

</RelativeLayout>

行的 xml 是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              >

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_albumTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="album title" />

  </LinearLayout>

  <ImageView
      android:id="@+id/imageView1"
      android:layout_width="105dp"
      android:layout_height="100dp"
      android:src="@drawable/abs__ab_share_pack_holo_light" />

</LinearLayout>
4

1 回答 1

2

这就是我的做法

你的 gridView.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/home_grid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:listSelector="@drawable/holo_blue_color"
        android:numColumns="2"
        android:layout_marginTop="10dip"
        android:verticalSpacing="10dip" >
    </GridView>

</LinearLayout>

现在你的 gridView 项目 xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="3dp"
    android:textColor="#000000"
    android:textSize="16dip"
    android:textStyle="normal" >

</TextView>

现在我做了一个适配器类来设置图像和文本

class MyGridAdapter extends BaseAdapter {

        ArrayList<String> name;
        ArrayList<Integer> resId;
        LayoutInflater inflater;

        public MyGridAdapter(Context context, ArrayList<String> name,
                ArrayList<Integer> resId) {
            this.name = name;
            this.resId = resId;

            inflater = LayoutInflater.from(context);
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return name.size();
        }

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return resId.get(arg0);
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.grid_textview, null);

            TextView tv = (TextView) convertView.findViewById(R.id.textview);
            tv.setText(name.get(position));
            tv.setCompoundDrawablesWithIntrinsicBounds(0, resId.get(position),
                    0, 0);

            return convertView;
        }

    }

您不需要包含 ImageView。您可以使用围绕 TextView 对齐图像setCompoundDrawablesWithIntrinsicBounds

现在您的活动中只有两行

final MyGridAdapter adapter = new MyGridAdapter(this, name, resId);

gridView.setAdapter(adapter);
于 2013-01-03T08:55:08.070 回答