繁体   English   中英

对齐并以线性布局包装图像按钮

[英]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 但是它扩展了可绘制对象的纵横比。

我应该如何使用布局将它们布置在视图的底部,等距分布,居中放置,并且没有可拉伸对象的任何长宽比。

您可以通过以下两种方式进行尝试

1)layout_weight和weightSum

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

您必须像这样使用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没问题,请尝试此...

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

尝试-

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

暂无
暂无

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

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