怎样的android伸展排在GridView

怎样的android伸展排在GridView

本文介绍了怎样的android伸展排在GridView,以填补屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的GridView(有一列和六排),在该单元格中显示的ImageView与TextView的(我创建适配器)。如何伸展行以填充整个屏幕高度?现在我有低于细胞一些空间...

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>

item.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;
}

}

推荐答案

由于我的理解,问题是 - 如果我有6行数据被填充在GridView中,如何才能让这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是(NO.OF你必须填写行)。这个最小高度可以在您正在使用的网卡进行设置。

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;

发现,在活动这些价值观,使他们的公共静态和使用它们的适配器,我在适配器类发现这些价值观出现了问题。

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,以填补屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:37