LinearLayout,四等分,左右靠边。

本文探讨在不使用代码仅依赖布局的情况下,如何实现四个UI元素与上部横线等长,同时确保元素间的间距相等且内容居中对齐。通过三种方案的对比,详细解析了每种方法的实现细节与局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

飞机航班详情
最近有这么一个UI需求。

1. 下面的四个item和上面的横线等长
2. item的key和value居中对齐
3. 同时四个item间距等分

不用代码,纯靠布局实现

方案一:

  1. 上面一个LinearLayout,下面一个LinearLayout。
  2. 四个item分别使用weight属性=1
  3. 同时把左右的item靠边显示。
  • 代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_36_px2dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/dimen_55_px2dp"
        android:orientation="horizontal">

        <TextView
            style="@style/text25gray"
            android:layout_weight="1"
            android:text="@string/check_in_counter" />

        <TextView
            style="@style/text25gray"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/boarding_port" />

        <TextView
            style="@style/text25gray"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/arrival_port" />

        <TextView
            style="@style/text25gray"
            android:layout_weight="1"
            android:gravity="end"
            android:text="@string/baggage_carousel" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/flight_information_check_in_counter"
            style="@style/text30white"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/flight_information_boarding_port"
            style="@style/text30white"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/flight_information_arrival_port"
            style="@style/text30white"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/flight_information_baggage_carousel"
            style="@style/text30white"
            android:layout_weight="1"
            android:gravity="end" />

    </LinearLayout>
  • 效果1:效果1

  • 缺点:

  1. 左右两个item的key和value没有居中对齐
  2. 四个item间距不等(因为左右的item靠边了,其实weight属性设置下应该距边有段距离的)

方案二

  1. 分为四个item,上下合成一个居中对齐
  2. 四个item使用weight属性=1
  • 代码2

<LinearLayout

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/check_in_counter" />

        <TextView
            android:id="@+id/flight_information_check_in_counter"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>

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

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/boarding_port" />

        <TextView
            android:id="@+id/flight_information_boarding_port"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>

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

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/arrival_port" />

        <TextView
            android:id="@+id/flight_information_arrival_port"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>

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

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/baggage_carousel" />

        <TextView
            android:id="@+id/flight_information_baggage_carousel"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>

  • 效果2效果2

类似于这种:
类似于这种

  • 缺点

  1. 居中是居中了,左右没有靠边。
那么做到这里我们知道,使用weight=1属性左右不靠边;左右靠边了,item不等分。那么,怎么解决呢?

办法①,把总长度扩展,使得左右两边对齐上面的边边
(不想扩展长度,得改变布局,还得不断调试,很费劲。而且如果文字长度发生变化,效果也不理想。)

办法②,固定每个item的宽度,从左到右排列,加左边距
(太傻。不灵活,即便在代码中动态设置宽度,也麻烦。我想在布局文件中就解决。)

最后,方案三

在这里插入图片描述

思路过程

  • 代码3

<LinearLayout
        android:id="@+id/linear_layout_check_in_counter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:orientation="vertical">

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/check_in_counter" />

        <TextView
            android:id="@+id/flight_information_check_in_counter"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_baggage_carousel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:orientation="vertical">

        <TextView
            style="@style/text25gray"
            android:layout_height="@dimen/dimen_36_px2dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/baggage_carousel" />

        <TextView
            android:id="@+id/flight_information_baggage_carousel"
            style="@style/text30white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dimen_13_px2dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/flight_information_baggage_carousel"
        android:layout_toRightOf="@id/flight_information_check_in_counter"
        android:orientation="horizontal"
        tools:ignore="NotSibling">

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

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

            <TextView
                style="@style/text25gray"
                android:layout_height="@dimen/dimen_36_px2dp"
                android:layout_gravity="center_horizontal"
                android:text="@string/boarding_port" />

            <TextView
                android:id="@+id/flight_information_boarding_port"
                style="@style/text30white"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/dimen_13_px2dp" />
        </LinearLayout>

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/linear_layout_baggage_carousel"
        tools:ignore="NotSibling">

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

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

            <TextView
                style="@style/text25gray"
                android:layout_height="@dimen/dimen_36_px2dp"
                android:layout_gravity="center_horizontal"
                android:text="@string/arrival_port" />

            <TextView
                android:id="@+id/flight_information_arrival_port"
                style="@style/text30white"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/dimen_13_px2dp" />
        </LinearLayout>

    </LinearLayout>
  • 效果3效果3

最后结果:
在不改总宽的情况下纯靠布局,基本实现。
瑕疵就是第一和第二个item间距还是有点大。
还有瑕疵,欢迎指正,一起探讨。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值