android 如何在gridview 中拉伸行以填充屏幕? [英] android How to stretch rows in the gridview to fill screen?
问题描述
我有简单的 GridView(一列六行),它在单元格中显示带有 TextView 的 ImageView(我创建了适配器).如何拉伸行以填充整个屏幕高度?现在我在单元格下方有一些空间...
I have simple GridView (with one column and six rows), that displays ImageView with TextView in the cell (I create adapter). How to stretch rows to fill entire screen height?? Now I have some space below cells...
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="150dp"
android:orientation="vertical"
android:id="@+id/left">
<include layout="@layout/menu_grid"></include>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/right">
<ImageView android:src="@drawable/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imageMain"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
menu_grid.xml
menu_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<GridView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/gridView"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
项目.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="fill_horizontal">
<ImageView android:src="@drawable/icon"
android:layout_height="70dp" android:id="@+id/imageIcon"
android:layout_width="70dp" android:layout_gravity="center_horizontal"></ImageView>
<TextView android:id="@+id/textIcon" android:text="TextView"
android:layout_gravity="center" android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
ImageAdapter.java
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
//....some code
//....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item, null);
TextView text = (TextView) view.findViewById(R.id.textIcon);
text.setText(labels[position]);
ImageView image = (ImageView) view.findViewById(R.id.imageIcon);
image.setImageResource(icons[position]);
} else {
view = convertView;
}
return view;
}
}
推荐答案
据我所知,问题是——如果我在 gridView 中有 6 行数据要填充,我怎样才能让这 6 行填充整个屏幕,而不是显示 6 行和最后一行下方的一些空白区域.
As of what i understood, the question is- if i have 6 rows of data to be filled in the gridView, how can i make these 6 rows fill the whole screen, instead of showing 6 rows and some empty space under the last row.
要实现这一点,您必须将 gridView 项目布局(在您的情况下是 item.xml)的最小高度设置为(屏幕高度)/6,其中 6 是(您必须填充的行数).这个最小高度可以在您使用的适配器中设置.
To achieve this you must set minimum height of the gridView item layout (in your case its item.xml) to (height of the screen)/6, where 6 is the (no.of rows you have to fill). This minimum height can be set in the adapter you are using.
要查找设备屏幕的高度:
To find the height of the screen of the device:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
在 Activity 中找到这些值并将它们设为 public static 并在适配器中使用它们,我在适配器类中找到这些值时遇到问题
find these values in the Activity and make them public static and use them in the adapter, i had a problem in finding these values in the adapter class
然后设置最小高度:
view = inflater.inflate(R.layout.item, null);
view.setMinimumHeight(GridViewActivity.height/6);
我在看到帖子后尝试了这个,效果很好.
I tried this after seeing the post, and it worked perfectly.
这篇关于android 如何在gridview 中拉伸行以填充屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!