我希望我的gridview行放在中心(水平)
我有两个宽度相同的列
每个项目将放置在相应列的中心。
我试着追随其他答案。他们都没有工作。 如果简单的布局没有按预期工作那么令人沮丧...
<?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>
答案 0 :(得分: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 Items 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。您可以使用setCompoundDrawablesWithIntrinsicBounds
现在你的活动只有两行
final MyGridAdapter adapter = new MyGridAdapter(this, name, resId);
gridView.setAdapter(adapter);