Android 如何设置网格视图的屏幕大小以匹配所有屏幕大小?

Android 如何设置网格视图的屏幕大小以匹配所有屏幕大小?,android,xml,gridview,Android,Xml,Gridview,这里我使用了一个gridview,但它不会随着屏幕大小而改变。我认为高度有问题。下面给出了gridview使用的xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_pa

这里我使用了一个gridview,但它不会随着屏幕大小而改变。我认为高度有问题。下面给出了gridview使用的xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical" >




<GridView
    android:id="@+id/gridView1"
     android:numColumns="3"
     android:gravity="center"
    android:paddingTop="20dp"
     android:horizontalSpacing="10dp"
     android:verticalSpacing="10dp"
     android:stretchMode="columnWidth"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/linearLayout1"
    android:layout_centerVertical="true"
     >
</GridView>


<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <com.assuretech.ku.HorizontalMenuBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>



据我所见。您需要在最后关闭RelativeLayout。我已经包括了你代码的截图。我将背景改为红色以查看文本。

我猜您正在寻找

numColumns=“自动安装”


作为GridView属性

首先,您必须使用一些布局来放置GridView。请执行以下操作:

制作布局的高度和宽度以填充父级:

android:layout\u width=“fill\u parent” android:layout\u height=“fill\u parent” 然后,设置gridview的高度和宽度以填充父视图

嗯,这只是一个假设的解决方案,你可能一直在这样做

希望这对您有用。


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp">

    <GridView
        android:id="@+id/grid_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="15dp">

    </GridView>

</LinearLayout>

我已经关闭了relativelayout标记……我在这里错过了它……它必须针对所有屏幕大小进行设置……对您的解决方案进行一些解释会很有用。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp">

    <GridView
        android:id="@+id/grid_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="15dp">

    </GridView>

</LinearLayout>