我有一个GridView
这样的(main.xml
):
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:background="@color/black"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp" >
</GridView>
和这样的GridView
项目(grid.xml
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:background="@color/white" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:src="@drawable/person" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/grid_item_image"
android:text="@+id/label"
android:layout_alignLeft="@id/grid_item_image"
android:layout_alignRight="@id/grid_item_image"
android:layout_marginTop="5dp"
android:textSize="15dp"
android:gravity="center"
android:textColor="@color/black" >
</TextView>
</RelativeLayout>
由于我只有 2 行项目GridView
,我希望这两行完全填满屏幕,不留下任何空白空间或不超出屏幕高度,这需要滚动才能完全看到。为此,我做了这个(GridViewActivity.java
):
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
在我的适配器(ImageAdapter.java
)中:
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid, null);
gridView.setMinimumHeight(GridViewActivity.height/2);
gridView.setMinimumWidth(GridViewActivity.width/2);
这并没有给出我想要的:
你可以看到第二行不完全可见
任何人都可以帮助我如何使这 2 行完全适合屏幕,或者如何在GridView
调用 Activity 时使其显示为中心,就像这样: