繁体   English   中英

带有图像按钮的Android XML布局

[英]Android XML Layout with imagebuttons in a row

我试图创建一行图像按钮,所以可以说3行和3列。

我一直试图使图像大小相同,但设计却搞砸了。

试图得到这样的东西:

图片图片图片

图片图片图片

图片图片图片

有人可以帮我弄清楚这些吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="172dp"
        android:id="@+id/imageButton"
        android:src="@drawable/sample_0"
        android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="129dp"
        android:layout_height="180dp"
        android:id="@+id/imageButton2"
        android:src="@drawable/sample_1" />

</LinearLayout>

如果您不想使用GridView ,则可以使用android:layout_weight ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="180dp">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton2"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton3"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="180dp">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton4"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton5"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton6"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="180dp">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton7"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton8"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton9"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

您不应该尝试使用layout_ height-width参数来排列图像。 随着屏幕尺寸的变化,方向等会导致布局混乱。

使用GridView在屏幕上排列图像。 请参阅gridview的文档GridView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM