如何在xml中设置gridlayout中每行的高度

时间:2015-01-20 15:00:01

标签: android xml

大家好我是android的新手。我想使用一个包含1列和4行的GridLayout来构建我的应用程序。

   <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="1"
    android:rowCount="4"
    tools:context=".MainActivity">

</GridLayout>

唯一的问题是我希望我的行具有不同的高度。有没有办法为xml中的每一行指定这个?

1 个答案:

答案 0 :(得分:0)

是的,你可以!

您需要创建一个CustomGridAdapter,它允许您指定每行的大小: 你应该看看这个教程: http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76&aaid=100

创建grid_item.xml之后,您可以在java中编辑应用程序OnCreate()中每行的大小。

例如: 将grid_item.xml设置如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginRight="10px"
    android:src="@drawable/android_logo" >

</ImageView>

然后在CustomGridAdapter中,您可以设置每个ImageView的宽度/高度,&#34; wrap_content&#34; linearLayout表示您的行将使用ImageView的大小。

干杯!