在Android中对齐两个文本视图的基线

时间:2014-11-08 22:35:59

标签: android textview

请在下面找到我的布局&结果;我需要文本“短信”来对齐文字“标题”的基线(请在下面的代码和快照中找到)。如果有人可以给我指示,我会很高兴

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:baselineAligned="true"
>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:background="@color/custom_orange"
    android:gravity="center"
    >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Header"
        android:textSize="25sp"
        android:id="@+id/title"
        android:gravity="center"
        android:background="@color/custom_red"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Text message"
        android:textSize="10sp"
        android:id="@+id/language"
        android:gravity="center"
        android:background="@color/custom_green"/> 
</LinearLayout>

</LinearLayout>

这是结果

enter image description here

这就是我需要的

enter image description here

4 个答案:

答案 0 :(得分:29)

使用RelativeLayout作为TextViews的容器并添加属性

android:layout_alignBaseline="@+id/title"

id / language TextView

答案 1 :(得分:4)

只需使用android:gravity =&#34; bottom&#34;在LinearLayout(与两个textviews的线性)和android:layout_height =&#34; wrap_content&#34;在两个TextViews中。

答案 2 :(得分:1)

在“组件”树上,选择要位于同一基础中的两个组件,右键单击,然后选择“显示基线”。只需拖动两个组件的基线即可。

答案 3 :(得分:-1)

对于 ConstraintLayout ,只需使用此↓与另一个textView基线对齐:

app:layout_constraintBaseline_toBaselineOf="@+id/textView"
相关问题