zoukankan      html  css  js  c++  java
  • 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>

    阅读原文

  • 相关阅读:
    console一闪而过的解决办法
    关于return
    各种js验证(笔记本)
    订单号的生成
    linux之cp/scp命令+scp命令
    11111111111
    Servlet中Web.xml文件的配置
    maven pom.xml详解
    环形缓冲区类(C++)附测试程序(转)
    环形缓冲区类(C++)绝对可用(转)
  • 原文地址:https://www.cnblogs.com/dmtyoung/p/6627607.html
Copyright © 2011-2022 走看看