android gridview适合屏幕大小

时间:2011-12-15 12:33:39

标签: android gridview

我的应用程序的gridview在每个网格下面都有一个图像和一个文本。 我需要确保完整的网格列表适合屏幕(底部没有溢出)。

以下是我定义gridview的方法

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:numColumns="3"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/text1view" 
                     android:gravity="center"
                    android:background="@drawable/home_bg"

                                />

并且每个网格项目是:

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

   <ImageView android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="0dip"
      android:layout_margin="0dip"
      >
   </ImageView>

   <TextView android:id="@+id/grid_item_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
           android:textColor="#000000"
           android:padding="0dip"
      android:gravity="center_horizontal"
      android:layout_below="@+id/grid_item_image"
      android:layout_alignParentBottom="true"
      >
   </TextView>

</LinearLayout>

我已经尝试了几种方法来设置网格视图布局参数,但我没有得到正确的输出。请帮帮我。 gridview适配器代码是

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.griditems, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView
                .findViewById(R.id.grid_item_text);
        Log.i(TAG, holder.text1.toString());
        holder.image = (ImageView) convertView
                .findViewById(R.id.grid_item_image);

        // if it's not recycled, initialize some attributes
        holder.image.setImageResource(gridItemIds[position]);
        holder.text1.setText(gridTitles[position]);
        int h = mContext.getResources().getDisplayMetrics().densityDpi;
        convertView.setLayoutParams(new GridView.LayoutParams(h-50, h-20));
        //holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //holder.image.setScaleType(ImageView.ScaleType.FIT_XY); ......

2 个答案:

答案 0 :(得分:0)

试试这个: -

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
android:layout_height="match_parent">
 <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center">
<ImageView android:id="@+id/imgIcon" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:text="TextView" android:id="@+id/txtDesc"   android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>   
</LinearLayout>

gridview.xml

<?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"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mobGrid"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>

答案 1 :(得分:-1)

尝试

android:layout_width="fill_parent"
android:layout_height="fill_parent"

用于GridView而不是wrap_content!