【问题标题】:Align two TextViews vertically so they are the exact same width垂直对齐两个 TextView,使它们的宽度完全相同
【发布时间】:2021-10-24 20:01:36
【问题描述】:

如果我有两个垂直对齐的文本视图,其中一个或另一个可能包含较长的文本,我如何垂直对齐它们,以便它们的背景图像看起来像是两个 TextView 的一个完整背景(所以一个大框不管哪些视图包含较长的文本)

原因是我在图片上使用了 textviews 但需要遮蔽它们以防图片与 textview 具有相同的颜色

更新:

正如 cmets 建议的那样,我现在使用了这样的线性布局,但是现在在使用 ConstraintLayout 时文本视图之间存在非常小的间隙。知道如何解决吗?

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginEnd="10dp">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_overlay_top"
        android:paddingStart="6dp"
        android:paddingTop="6dp"
        android:paddingEnd="6dp"
        android:text="TextView1"
        android:textColor="?colorOnPrimary"
        android:textSize="13sp"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="14dp"
        android:background="@drawable/bg_overlay_bottom"
        android:paddingStart="6dp"
        android:paddingEnd="6dp"
        android:paddingBottom="6dp"
        android:text="TextVie2 long"
        android:textColor="?attr/colorOnPrimary"
        android:textSize="16sp"
        android:textStyle="bold"/>

</androidx.appcompat.widget.LinearLayoutCompat>

更新 2

我现在使用一个完整的图像作为线性布局的背景,但遗憾的是这个解决方案也不起作用,线性布局只限制下部文本视图,这意味着如果下部比上部短,则上部被截断

【问题讨论】:

    标签: android textview


    【解决方案1】:

    无论哪个视图的文本较长,如何让两个 TextViews 具有相同的宽度。

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray"
            android:text="Here is some long, long text."
            android:textSize="28sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@id/barrierEnd"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_min="wrap" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:text="Bottom"
            android:textSize="28sp"
            app:layout_constraintEnd_toEndOf="@id/barrierEnd"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView1"
            app:layout_constraintWidth_min="wrap" />
    
        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrierEnd"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:barrierDirection="end"
            app:constraint_referenced_ids="textView1,textView2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    此解决方案是从here 借来的。我试图解释它为什么会起作用here

    【讨论】:

    • app:layout_constraintWidth_min="wrap" 这是关键。谢谢你。我尝试了app:layout_constraintWidth="wrap_content",但没有成功。我不知道还有这样的另一个属性。所以不需要嵌套布局。不错
    【解决方案2】:

    如您所说,对于一个大蓝色背景,您可以使用垂直 LinearLayout 作为两个 TextView 的容器,并将其背景颜色设置为蓝色。

    【讨论】:

    • 其实。是的!有时它就是这么简单。我只使用过我不再考虑所有旧布局的 ConstraintLayout。它有效,但有一个问题:两个 TextView 之间的差距非常小,因此背景似乎不再是一个。我发布和更新
    • 我通过使用线性布局的完整图像作为背景解决了这个问题。谢谢
    • 其实这也行不通。如果您尝试将两个具有匹配父项的文本视图放入带有换行内容的线性布局中,则无论上部文本视图有多长,线性布局都将跨越下部文本视图的宽度
    【解决方案3】:

    如果您使用的是垂直线性布局,您可以将 android:layout_width (of the TextViews) 设置为“match_parent”而不是“wrap_content”。

    编辑:在外部垂直 LinearLayout 中放置另一个垂直 LinearLayout 与 android:layout_width="wrap_content" 和 2 TextViews 里面与 android:layout_width="match_parent"

    【讨论】:

    • 不,他们不应该与父母相匹配。由于它们的背景图像,它们应该包装内容。背景图像应该只位于文本后面,并且跨越该区域到其他文本的末尾。
    • 啊,现在我想你和 Wakil 有同样的想法。是的,这几乎可以工作,但它是如此简单。查看编辑
    【解决方案4】:

    如果您使用的是约束布局,那么 app:layout_constraintStart_toStartOf

    会为你工作,同时使用baseline 也会非常有用。

    【讨论】:

    • 我正在使用约束布局,我知道如何对齐两者的开头。这里的问题是如何对齐两端,以便无论哪个更长,它们总是对齐
    • app:layout_constraintEnd_toEndOf 不会工作吗?如果没有,你能详细解释一下吗?
    • 这不起作用,因为其中任何一个文本视图都可能比另一个更长。所以两者都必须被限制在另一个的末端,并且两者的宽度都是 0dp。但这不起作用,因为 api 需要至少一个长度来定位另一个的 0dp
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2012-07-05
    • 1970-01-01
    • 2015-04-07
    相关资源
    最近更新 更多