简体   繁体   English

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

[英]Align and wrap imagebuttons in linearlayout

I have a linearlayout that has imagebuttons aligned horizontally, at the bottom of the parent layout. 我有一个linearlayout ,其imagebuttons在父布局的底部水平对齐。 This is exactly, what I want it to look like: 这正是我想要的样子:

在此处输入图片说明

This is how I am doing it: 这就是我的做法:

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

I have also tried to give weightsum to the linearlayout , and assign weights to imagebuttons . 我还尝试过将weightsum分配给linearlayout ,并将weights分配给imagebuttons But it stretches the aspect ratio of the drawables. 但是它扩展了可绘制对象的纵横比。

How should I use my layout to arrange them in the bottom of the view, evenly spaced, centrally placed and without any stretched out aspect ratios of drawables.. 我应该如何使用布局将它们布置在视图的底部,等距分布,居中放置,并且没有可拉伸对象的任何长宽比。

You can try in two ways as bellow 您可以通过以下两种方式进行尝试

1) layout_weight & weightSum 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) Either change to 'RelativeLayout' 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>

You have to used RelativeLayout like this..... 您必须像这样使用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>

If you want to achieve this one using LinearLayout try this one.. 如果要使用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>

Or if you are ok with relativelayout try this one... 或者,如果您对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>

Try - 尝试-

<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