【问题标题】:Left view will take up all the space in a horizontal LinearLayout左视图将占用水平 LinearLayout 中的所有空间
【发布时间】:2021-10-18 06:01:52
【问题描述】:

我有两个 TextView(tv1tv2),我希望 tv2 位于 tv1 的右侧,如下所示:

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 将占据所有空间:

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 的宽度,但是除了编程还有其他方法吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    最后我设法通过使用 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 的顶部(和内部)。

    【讨论】:

    • 虽然使用布局权重是正确的答案,但您很可能并不完全了解它的工作原理,否则您不会将其设置为 0.5,因为该值在这里并不直接重要,而是与其他值成比例重量是最重要的。将其设置为相同的值,即最明显的只是 1,但 5 或 13 的工作方式相同。所以它的工作原理不是因为你将它设置为 0.5,而是你将两者都设置为相同
    • 感谢您的回答。我知道layout_weight,它用于设置每个视图的大小比例,但我希望tv2可以接近tv1。将layout_weight 设置为相同的值会使tv1tv2 各占一半的空间,这不是我想要的
    • @BoomingBones 很抱歉我忽略了android:text="*"。我用 ConstraintLayout 更新了我的答案。
    • 谢谢,但这仍然不是我想要的。我编辑了我的问题并添加了一些图片,请看一下
    • @BoomingBones 最后,我重写了我的答案。请检查一下。
    【解决方案2】:

    试试这个它对我有用。您必须在两个 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>
    

    【讨论】:

      【解决方案3】:

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

      1. LinearLayout 更改为TableLayout + TableRow

      2. 使用TableLayoutshrinkColumns 属性指定要收缩的列的从零开始的索引。我将它设置为0,因为我希望 tv1 可以收缩,这样它就不会占用所有空间

        <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>
        

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 2017-04-16
        • 2013-03-07
        • 2022-01-21
        相关资源
        最近更新 更多