美文网首页
两个TextView,第二个不超过最右侧边距

两个TextView,第二个不超过最右侧边距

作者: JasonChen8888 | 来源:发表于2018-09-27 10:14 被阅读0次

最近一个UI上的需求,如图:

列表需求.png
  • 描述
    1、最右侧的时间必须全部展示。
    2、“今日”的标志必须跟随着左侧的标题,但是在文字很多的情况,不能超过右侧的最左边的字
    3、最左侧的标题不够展示显示... (这个easy)

  • 思路
    1、这种需求下,想到的父布局是RelativeLayout
    2、右侧直接国定在最右侧,让时间展示在父布局最右侧android:layout_alignParentRight="true"
    3、左侧,因为要让今日的这个时间不能超过右侧的时间,即将标题和“今日”标志放在一个LinearLayout中
    4、LinearLayout中,标题展示的view控件设置权重为1(layout_weight = "1")
    5、LinearLayout这个布局设置位于右侧的左边,即完成。layout_toLeftOf="@+id/tv_time"

  • 代码

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_time">

        <LinearLayout
            android:id="@+id/ll_server"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

            <TextView
                android:id="@+id/tv_server"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="end"
                android:lines="1"
                android:paddingBottom="@dimen/space_17"
                android:paddingLeft="@dimen/space_10"
                android:paddingRight="@dimen/space_6"
                android:paddingTop="@dimen/space_17"
                android:singleLine="true"
                android:textColor="@color/font_deep_gray"
                android:textSize="@dimen/font_size_14"
                tools:text="文本" />

            <TextView
                android:id="@+id/tv_open_server_flag"
                android:layout_width="27dp"
                android:layout_height="@dimen/space_15"
                android:layout_marginRight="@dimen/space_6"
                android:background="@drawable/bg_game_detail_sever_txt"
                android:gravity="center"
                android:text="@string/txt_game_server_today"
                android:textColor="@color/game_server_today_flag_color"
                android:textSize="@dimen/font_size_11" />
        </LinearLayout>
    </RelativeLayout>

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/space_10"
        android:gravity="right"
        android:textColor="@color/font_assist_deep_gray"
        android:textSize="@dimen/font_size_14"
        tools:text="123156" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="@dimen/space_10"
        android:layout_marginRight="@dimen/space_10"
        android:background="@color/line_gray" />
</RelativeLayout>

相关文章

网友评论

      本文标题:两个TextView,第二个不超过最右侧边距

      本文链接:https://www.haomeiwen.com/subject/eensoftx.html