使网格视图内容集中在每个单元格中

时间:2014-04-19 08:58:19

标签: java android gridview

在xml中使用此代码进行gridview

 <GridView
    android:id="@+id/gridviewmain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/searchView1"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:numColumns="3"
    android:padding="1dp"
    android:verticalSpacing="1dp"
    android:listSelector="@drawable/list_selector"
    android:choiceMode="singleChoice">
</GridView>

我似乎无法集中每个单元格中的内容,无论我做什么,似乎都是左对齐的! 有什么建议 ?

1 个答案:

答案 0 :(得分:0)

这是我正在使用的自定义适配器

public class SectionAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public SectionAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

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

    TextView sectionid = (TextView)vi.findViewById(R.id.txtItemID);
    TextView sectionname = (TextView)vi.findViewById(R.id.txtItemname); 
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.imageviewItem); 

    HashMap<String, String> showdata = new HashMap<String, String>();
    showdata = data.get(position);

    sectionid.setText(showdata.get(MainActivity.TAG_SECTION_ID));
    sectionname.setText(showdata.get(MainActivity.TAG_SECTION_NAME));
    imageLoader.DisplayImage(showdata.get(MainActivity.TAG_SECTION_IMAGE), thumb_image);
    return vi;
}

这是gridview

中每个项目的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom2color"
    android:orientation="vertical"
    android:padding="3dip"
    android:gravity="center">

    <TextView
        android:id="@+id/txtItemname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageviewItem"
        android:layout_width="150dp"
        android:layout_height="150dip"
        android:gravity="center"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<TextView
    android:id="@+id/txtItemID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="id"
     android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/transparent" />