左视图将占据水平 LinearLayout 中的所有 space

Left view will take up all the space in a horizontal LinearLayout

我有两个 TextView(tv1tv2),我希望 tv2tv1[ 的右边=47=],像这样:

short text

long text

(对不起,我无法嵌入图片,因为我没有足够的声誉:(

所以我做了这样的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="*" />

</LinearLayout>

如果tv1text在一行之内,就完美了。但是如果 text 多于一行, tv1 将占用所有 space:

tv1 take all the space

我想可能是因为Android按照布局文件中出现的顺序布局视图,所以我将根视图从LinearLayour更改为ConstraintLayout并制作tv2 第一:

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="*"
    app:layout_constraintLeft_toRightOf="@+id/tv1"
    app:layout_constraintStart_toEndOf="@+id/tv1"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这还是不行。我知道我可以使用 setLayoutParams 以编程方式设置 tv1 的宽度,但是除了编程之外还有其他方法吗?

最后我设法通过使用 padding:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="12sp"
        android:text="HelloWorld"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="12sp"
        android:layout_height="wrap_content"
        android:text="*"
        app:layout_constraintRight_toRightOf="@id/tv1"
        app:layout_constraintTop_toTopOf="@id/tv1" />

</androidx.constraintlayout.widget.ConstraintLayout>

重点是使用 tv2 的从右到右约束 tv1,并设置固定大小的 tv2 宽度和 tv1 的 padding-right 大小。所以 tv2 覆盖在 tv1 的顶部(和内部)。

试试这个对我有用。您必须在两个 TextView 中设置 layout_width="0dp" 和 layout_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/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:textSize="20dp"
        android:text="hello world ********** welcome"/>


    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20dp"
        android:text="hello world ********** welcome" />

</LinearLayout>

终于找到了一个完美的存档方式:

  1. LinearLayout改为TableLayout+TableRow

  2. 使用TableLayoutshrinkColumns属性指定要收缩的列的从零开始的索引。我设置为0是因为我想让tv1可以收缩,这样就不会占满space

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="*" />
    
        </TableRow>
    </TableLayout>