【问题标题】:using match parent in grid layout在网格布局中使用匹配父级
【发布时间】:2014-05-07 14:44:20
【问题描述】:

您好,我的应用程序中的一个活动有问题,它没有填满所有可用空间这里是屏幕截图

现在这里看起来不错,它的右侧是一个相对布局,里面有按钮,一个网格布局包含所有动物当点击相对布局中的按钮时,它会更改网格布局中的图像,但是当它显示在平板电脑上时,动物占据左上角而不是拉伸以满足其他布局我的问题是我可以做些什么来使网格布局填充这个空间还是设置网格视图更实用? 这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MusicActivity"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/secondLayout"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true">

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/Button1"
        android:background="@drawable/choose_animals2"/>

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/Button2"
        android:background="@drawable/choose_letters"
        android:layout_below="@+id/Button1"
        android:layout_alignParentLeft="true" />

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/Button3"
        android:background="@drawable/piano_icon"
        android:layout_below="@+id/Button2"
        android:layout_alignParentLeft="true" />
        <Button
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:id="@+id/Button4"
            android:background="@drawable/drum_icon0"
            android:layout_below="@+id/Button3"
            android:layout_alignParentLeft="true" />
        <Button
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:id="@+id/Button5"
            android:background="@drawable/choose_numbers"
            android:layout_below="@+id/Button4"
            android:layout_alignParentLeft="true" />
        <Button
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/nxtbtn"
            android:id="@+id/Button6"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" />
    </RelativeLayout>

    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:fillViewport="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/secondLayout">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2"
        android:rowCount="13">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:layout_column="0"
            android:layout_row="0"
            android:padding="5dp"
            android:background="@drawable/lion_icon"
            android:contentDescription="@string/lionhead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton2"
            android:layout_column="1"
            android:layout_row="0"
            android:background="@drawable/dog_icon"
            android:padding="5dp"
            android:contentDescription="@string/doghead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton3"
            android:background="@drawable/cat_icon"
            android:padding="5dp"
            android:layout_column="0"
            android:layout_row="1"
            android:contentDescription="@string/cathead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton4"
            android:layout_column="1"
            android:layout_row="1"
            android:padding="5dp"
            android:background="@drawable/duck_icon"
            android:contentDescription="@string/duckhead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton5"
            android:padding="5dp"
            android:background="@drawable/monkey_icon"
            android:layout_column="0"
            android:layout_row="2"
            android:contentDescription="@string/monkeyhead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton6"
            android:background="@drawable/sheep_icon"
            android:padding="5dp"
            android:layout_column="1"
            android:layout_row="2"
            android:contentDescription="@string/sheephead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton7"
            android:padding="5dp"
            android:background="@drawable/pig_icon"

            android:layout_column="0"
            android:layout_row="3"
            android:contentDescription="@string/pighead"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton8"
            android:padding="5dp"
            android:background="@drawable/ele_icon"

            android:layout_column="1"
            android:layout_row="3"
            android:contentDescription="@string/elephanthead"/>


        </GridLayout>
    </ScrollView>
</RelativeLayout>

感谢所有建议

【问题讨论】:

  • 发布您的布局 xml。
  • 你试过用 android:stretchMode="XXX" 做实验吗? / android:numColumns="auto_fit" / android:columnWidth="" 平板电脑屏幕尺寸
  • 我对 android 来说不是很新我的拉伸模式目前的网格布局是 collumnwidth @Hamid Shatu 更新以包含代码
  • 那是怎么编译的,android:orientation 只适用于LinearLayout 不是吗?
  • 是的,从前它是一个线性布局,高度和宽度现在提供方向,所以谢谢你我会把它拿出来,但我知道有一些问题,但我们可以请专注于问题

标签: android grid-layout


【解决方案1】:

刚找到这个(见下面我的最后一条评论),它在大多数情况下确实有效,只是基本上为我的网格的每一行坚持线性布局 让我想知道为什么需要网格布局对不起广泛的答案,但我刚刚添加了一个线性子视图来很好地显示图像,如此处所述http://developer.android.com/reference/android/widget/GridLayout.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多