简体   繁体   English

带有图像按钮的Android XML布局

[英]Android XML Layout with imagebuttons in a row

Im trying to create a row of image buttons, so lets say 3 rows and 3 columns. 我试图创建一行图像按钮,所以可以说3行和3列。

I keep trying to make the images all the same size but the design just messes up. 我一直试图使图像大小相同,但设计却搞砸了。

Trying to get something like this: 试图得到这样的东西:

Image Image Image 图片图片图片

Image Image Image 图片图片图片

Image Image Image 图片图片图片

Can someone help me get these in line? 有人可以帮我弄清楚这些吗?

<?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>

If you dont want to use GridView , you can do it with android:layout_weight , like that: 如果您不想使用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>

You should not try to arrange images with layout_ height-width parameters. 您不应该尝试使用layout_ height-width参数来排列图像。 As with change in screen size, orientation etc will result into messing up the layout. 随着屏幕尺寸的变化,方向等会导致布局混乱。

Use GridView to arrange your images on screen. 使用GridView在屏幕上排列图像。 See the docs for gridview GridView 请参阅gridview的文档GridView

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

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