android布局:TextView右侧的TextView

android layout: TextView on the right side of the TextView

我需要显示的布局:

  1. 如果cl__tv_team_name有短文本,它会在旁边显示 (Q) 符号 cl__tv_team_namecl__tv_team_name和(Q)号相邻)
  2. 如果cl__tv_team_name有长文本,textview会显示两行或更多行,但(Q)符号仍然可见右边

简单 LinearLayout 不起作用。 TextViewcl__tv_team_name占满宽度

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


    <TextView
        android:id="@+id/cl__tv_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" />

    <TextView
    android:id="@+id/cl__tv_q_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(Q)" />

</LinearLayout>

我也试过RelativeLayout没有效果:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

    <TextView
        android:id="@+id/cl__tv_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/cl__tv_team_name"
        android:layout_toRightOf="@+id/cl__tv_team_name"
        android:text="(Q)"
        android:textSize="13sp" />
</RelativeLayout>

尝试在相关布局中混合使用垂直线性布局和水平线性布局。听起来很混乱,但它通常有效。

这应该有效

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical">

        <TextView
            android:id="@+id/q_sign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="(Q)"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/cl__tv_team_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/q_sign"
            android:layout_toLeftOf="@+id/q_sign"
            android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" />

</RelativeLayout>

尝试在您的 LinearLayout 布局 xml 文件中添加 android:weight=1。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/cl__tv_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" />

    <TextView
        android:id="@+id/cl__tv_q_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(Q)" />
</LinearLayout>

我会使用 ConstraintLayout 来实现:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="short text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/q"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"/>

    <TextView
        android:id="@+id/q"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(Q)"
        app:layout_constraintLeft_toRightOf="@+id/text"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@+id/text"/>

</android.support.constraint.ConstraintLayout>

或者让第一个视图中的文本非常长: