将android布局与等边距对齐

时间:2016-12-19 13:09:48

标签: android android-layout android-layout-weight

我在我的应用中添加了一个页脚,其中包含三个带图像的按钮。我想要类似于下面的视图。我试过android:layout_gravity="right/left/center"但似乎没有用。

enter image description here

下面是我的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Root element should wrap to parent size. -->

<!-- Your view xml codes. -->

<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="4">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="left"
    android:orientation="vertical"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stores"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1"
    android:layout_gravity="right"
    >


    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"

        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"

        />


</LinearLayout>
</LinearLayout>


</RelativeLayout>

6 个答案:

答案 0 :(得分:2)

您的父母LinearLayoutandroid:weightSum="4",而您的孩子LinearLayout所有金额均为3。

所以将您的父LinearLayout weightSum android:weightSum="4"更改为android:weightSum="3"

更新:

以这种方式添加

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Root element should wrap to parent size. -->

<!-- Your view xml codes. -->

<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:weightSum="3">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
     >
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
   >
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stores"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1">
 <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"
        android:layout_gravity="center_horizontal"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands" 
        android:layout_gravity="center_horizontal"/>
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>

答案 1 :(得分:2)

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

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brands"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="brands"
/>

</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shops"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="shops"
/>

</LinearLayout>


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">


<ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/coupons"
/>

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="coupons"
/>

</LinearLayout>
</LinearLayout>

尝试这样的事情,

LinearLayout(Horizontal)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)
  - LinearLayout(Vertical)
        - ImageView (for the image)
        - Textview (for title)

答案 2 :(得分:2)

请试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:weightSum="3">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/brands" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="brands" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stores" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stores" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="5dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/coupons" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Coupons" />
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

答案 3 :(得分:2)

    public enum Container {
        PERSON1(new Person()),
        PERSON2(new Person()),
        PERSON3(new Person()),
        PERSON4(new Person()),
        PERSONN(new Person());

        private final Person singleton;

        Container(final Person person) {
            this.singleton = Objects.requireNonNull(person);
        }
        public Person get(){
            return singleton;
        }
    }

    public static void main(String[] args) {
        Person singletonA = Container.PERSON1.get();
        singletonA.setName("John");
        Person singletonB = Container.PERSON1.get();
        System.out.println(singletonB.getName());
    }

试试这个。我认为它会解决你的问题。

答案 4 :(得分:1)

给予布局相同的权重

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="24dp"
            android:src="@android:drawable/ic_menu_rotate"
            android:layout_height="24dp" />
        <TextView
            android:layout_width="wrap_content"
            android:text="@string/description"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

答案 5 :(得分:1)

更改所有内部布局

  

机器人:layout_gravity =&#34;左&#34;

android:layout_margin="10dp"//as required

并更改

  

机器人:weightSum =&#34; 3&#34;

父布局中的