在线性布局中对齐和包装图像按钮

Align and wrap imagebuttons in linearlayout

我有一个 linearlayout,其中 imagebuttons 水平对齐,位于父布局的底部。这正是我想要的样子:

我是这样做的:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:background="@color/dim_foreground_disabled_material_dark"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/new_1"
            android:background="@drawable/_dashboard_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageButton
            android:id="@+id/new_2"
            android:background="@drawable/_dashboard_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageButton
            android:id="@+id/new_3"
            android:background="@drawable/_dashboard_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

我也试过把weightsum赋给linearlayout,把weights赋给imagebuttons。但它拉伸了可绘制对象的纵横比。

我应该如何使用我的布局将它们排列在视图的底部,均匀分布,居中放置并且没有任何拉伸的可绘制对象的纵横比..

尝试 -

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="end|bottom"
                android:weightSum="3"
                android:background="@color/dim_foreground_disabled_material_dark"
                android:orientation="horizontal">


                <ImageButton
                    android:id="@+id/new_1"
                    android:src="@drawable/_dashboard_1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    android:background="@null"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true" />

                <ImageButton
                    android:id="@+id/new_2"
                    android:src="@drawable/_dashboard_2"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    android:background="@null"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true" />

                <ImageButton
                    android:id="@+id/new_3"
                    android:src="@drawable/_dashboard_3"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    android:background="@null"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"  />

            </LinearLayout>

你必须这样使用 RelativeLayout.....

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/dim_foreground_disabled_material_dark">

    <ImageButton
        android:id="@+id/new_1"
        android:background="@drawable/_dashboard_1"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageButton
        android:id="@+id/new_2"
        android:background="@drawable/_dashboard_2"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageButton
        android:id="@+id/new_3"
        android:background="@drawable/_dashboard_3"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

如果你想使用 LinearLayout 实现这个,试试这个..

   <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="10">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton10"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dip"
        android:background="@android:color/transparent"/>
   <View android:layout_width="0dip"
         android:layout_weight="5"
         android:layout_height="1dip"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton9"
        android:src="@mipmap/ic_launcher"
        android:background="@android:color/transparent"/>
    <View android:layout_width="0dip"
        android:layout_weight="5"
        android:layout_height="1dip"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton8"
        android:src="@mipmap/ic_launcher"
        android:layout_marginRight="20dip"
        android:background="@android:color/transparent"/>
</LinearLayout>

或者如果你对相对布局没问题,试试这个...

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:id="@+id/imageButton12"
        android:src="@mipmap/ic_launcher"
        android:background="@android:color/transparent"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton11"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher"
        android:background="@android:color/transparent"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton13"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher"
        android:background="@android:color/transparent"/>
</RelativeLayout>

您可以尝试以下两种方式

1) layout_weight & 权重总和

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:background="@color/dim_foreground_disabled_material_dark"
        android:orientation="horizontal" 
        android:weightSum="1">

        <Button
            android:id="@+id/new_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:text="A" />

        <Button
            android:id="@+id/new_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:text="B" />

        <Button
            android:id="@+id/new_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.33"
            android:text="C" />
    </LinearLayout>

2) 要么改为 'RelativeLayout'

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:background="@color/dim_foreground_disabled_material_dark"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/new_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  
             android:layout_alignParentLeft="true"                      
            android:text="A" />

        <Button
            android:id="@+id/new_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="B" />

        <Button
            android:id="@+id/new_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="C" />
    </RelativeLayout>