簡體   English   中英

以相等的邊距對齊android布局

[英]Align android layout with equal margin

我在我的應用程序中添加了一個頁腳,它將包含三個帶有圖像的按鈕。 我想要類似下面的視圖。 我嘗試了android:layout_gravity="right/left/center"但似乎無法正常工作。

在此處輸入圖片說明

下面是我的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>

你父母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>
<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)

請嘗試這個

<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>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 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:gravity="center"
        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:gravity="center"
        android:orientation="vertical">

        <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:layout_weight="1"
        android:gravity="center"
        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="brands" />

    </LinearLayout>
</LinearLayout>

嘗試這個。 我認為它將解決您的問題。

對布局給予同等的重視

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

更改所有內部布局

android:layout_gravity =“ left”

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

並改變

android:weightSum =“ 3”

在父級布局中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM