Android布局:宽度适应的横向跟随,防止挤掉重要视图

不知道这样的布局该怎么描述,标题也是乱取的。。直接上图吧

最近遇到了这样要求的布局:

1、上图中的“标题”长度不定,“状态”标签可能有多个并紧跟在标题右边,“属性”一直居右显示;

2、当“标题”过长,一行显示不下时,“标题”换行显示,但不能挤掉“状态”和“属性”。

 

刚开始用了LinearLayout和RelativeLayout的多层嵌套总算是强行实现,但是嵌套太深了,而且代码看着也相当复杂,所以修改为用TableLayout,利用shrinkColumns和stretchColumns保证跟随的视图不被挤掉。

 

当然还有一个问题没解决:固定居右的“属性”一直没能放进TableLayout里,尝试设置“属性”为 stretchColumns + match_parent + gravity: right,放到TableRow的最后,但是没有效果,所以这里才套了层RelativeLayout,如果有办法能把RelativeLayout这层都省去,麻烦大家告诉我下~

 

 1 <RelativeLayout
 2             android:layout_width="match_parent"
 3             android:layout_height="wrap_content"
 4             android:gravity="center"
 5             android:orientation="horizontal">
 6 
 7             <TableLayout
 8                 android:id="@+id/table"
 9                 android:layout_width="match_parent"
10                 android:layout_height="wrap_content"
11                 android:layout_toLeftOf="@+id/txvCreateTime"
12                 android:shrinkColumns="0"
13                 android:stretchColumns="1|2">
14 
15                 <TableRow android:gravity="center_vertical">
16 
17                     <TextView
18                         android:id="@+id/txvTitle"
19                         android:layout_width="wrap_content"
20                         android:layout_height="wrap_content"
21                         android:ellipsize="end"
22                         android:maxLines="2"
23                         android:text="居左居左居左居左居左居左"
24                         android:textColor="@android:color/black"
25                         android:textSize="@dimen/dimens_16_sp"
26                         android:textStyle="bold" />
27 
28                     <TextView
29                         android:id="@+id/txvState1"
30                         android:layout_width="wrap_content"
31                         android:layout_height="wrap_content"
32                         android:layout_marginLeft="@dimen/dimens_5_dp"
33                         android:background="@drawable/layout_bg_orange"
34                         android:paddingBottom="@dimen/dimens_2_dp"
35                         android:paddingLeft="@dimen/dimens_5_dp"
36                         android:paddingRight="@dimen/dimens_5_dp"
37                         android:paddingTop="@dimen/dimens_2_dp"
38                         android:singleLine="true"
39                         android:text="跟随"
40                         android:textColor="@android:color/white"
41                         android:textSize="@dimen/dimens_12_sp" />
42 
43                     <TextView
44                         android:id="@+id/txvState2"
45                         android:layout_width="wrap_content"
46                         android:layout_height="wrap_content"
47                         android:layout_marginLeft="@dimen/dimens_5_dp"
48                         android:background="@drawable/layout_bg_orange"
49                         android:paddingBottom="@dimen/dimens_2_dp"
50                         android:paddingLeft="@dimen/dimens_5_dp"
51                         android:paddingRight="@dimen/dimens_5_dp"
52                         android:paddingTop="@dimen/dimens_2_dp"
53                         android:singleLine="true"
54                         android:text="跟随"
55                         android:textColor="@android:color/white"
56                         android:textSize="@dimen/dimens_12_sp" />
57                 </TableRow>
58             </TableLayout>
59 
60             <TextView
61                 android:id="@+id/txvCreateTime"
62                 android:layout_width="wrap_content"
63                 android:layout_height="wrap_content"
64                 android:layout_alignParentRight="true"
65                 android:layout_centerVertical="true"
66                 android:layout_marginLeft="@dimen/dimens_5_dp"
67                 android:gravity="center"
68                 android:text="居右"
69                 android:textColor="#999999"
70                 android:textSize="@dimen/dimens_14_sp" />
71         </RelativeLayout>

 

阅读原文

转载于:https://www.cnblogs.com/dmtyoung/p/6627607.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值