简体   繁体   English

将视图与另一个 TextView 的基线对齐

[英]Align View to baseline of another TextView

I try to create a simple line that align at bottom after TextView.我尝试在 TextView 之后创建一条在底部对齐的简单线条。 I use RelativeLayout as a container with alignBaseLine.我使用RelativeLayout 作为带有alignBaseLine 的容器。 But the result is not what I need ( See Image Below) Unlike align two TextView, When I align TextView and View it's not align from bottom to top.但结果不是我需要的(见下图)与对齐两个 TextView 不同,当我对齐 TextView 和 View 时,它不是从下到上对齐的。 It's start from top of View to bottom.它从视图的顶部开始到底部。 Is there any way I can achieve this ?有什么办法可以做到这一点吗?

在此处输入图片说明

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Brand We Love"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignBaseline="@id/tvTitle"
        android:layout_gravity="bottom"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@+id/tvTitle"
        android:layout_toRightOf="@+id/tvTitle"
        android:background="@color/colorAccent" />

</RelativeLayout>

Given that android:layout_alignBaseline will position a View just below the baseline, you can use this constraint to place an invisible View there which will act as a guideline and then position your View above the guideline:鉴于android:layout_alignBaseline将使一个View只是低于基准,则可以使用此约束将一个无形的View有将作为一个准则,然后定位您View了上述方针:

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Brand We Love"
    android:textAllCaps="true"
    android:textColor="@color/colorAccent"
    android:textSize="18sp" />


    <View
        android:id="@+id/guideline"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignBaseline="@id/tvTitle"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@+id/tvTitle"
        android:layout_toRightOf="@+id/tvTitle"
        android:background="#00000000" />

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_above="@+id/guideline"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@+id/tvTitle"
        android:layout_toRightOf="@+id/tvTitle"
        android:background="@color/colorAccent" />

For ConstraintLayout set the baseline to the bottom and unite baselines.对于ConstraintLayout将基线设置为底部并合并基线。

In short - just add these lines to your View :简而言之 - 只需将这些行添加到您的View

android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@id/textView"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM