最近有这么一个UI需求。
1. 下面的四个item和上面的横线等长
2. item的key和value居中对齐
3. 同时四个item间距等分
不用代码,纯靠布局实现
方案一:
- 上面一个LinearLayout,下面一个LinearLayout。
- 四个item分别使用weight属性=1
- 同时把左右的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>
- 左右两个item的key和value没有居中对齐
- 四个item间距不等(因为左右的item靠边了,其实weight属性设置下应该距边有段距离的)
方案二
- 分为四个item,上下合成一个居中对齐
- 四个item使用weight属性=1
<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>
类似于这种:
- 居中是居中了,左右没有靠边。
那么做到这里我们知道,使用weight=1属性左右不靠边;左右靠边了,item不等分。那么,怎么解决呢?
办法①,把总长度扩展,使得左右两边对齐上面的边边
(不想扩展长度,得改变布局,还得不断调试,很费劲。而且如果文字长度发生变化,效果也不理想。)
办法②,固定每个item的宽度,从左到右排列,加左边距
(太傻。不灵活,即便在代码中动态设置宽度,也麻烦。我想在布局文件中就解决。)
最后,方案三
<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>
最后结果:
在不改总宽的情况下纯靠布局,基本实现。
瑕疵就是第一和第二个item间距还是有点大。