2014-11-08 37 views
9

請在下面找到我的佈局&其結果;我需要文字'短信'以對齊文字'標題'的底線(請在代碼和快照下方找到)。會很高興,如果有人可以給我方向在Android中對齊兩個文本視圖的基線

<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

回答

18

使用的RelativeLayout作爲TextViews容器並添加屬性

android:layout_alignBaseline="@+id/title" 

ID /語言的TextView

2

只需使用機器人:比重=上的LinearLayout 「底部」(具有兩個textviews線性)和機器人:layout_height = 「WRAP_CONTENT」 在兩個TextViews。

+1

當字體大小不同時,不解決OP的問題。 – javajon 2015-01-13 17:37:00

+0

其實它確實如此。當我意外地發現這一點時,我感到非常驚訝。現在我正在理解如何。 – 2016-04-18 19:28:27

+3

我知道這是非常古老的,但我會在這裏留下,因爲我認爲這很有幫助,LinearLayouts有一個名爲「baselineAligned」(默認開啓)的屬性,它完全按照它的名稱暗示:基於它們的視圖基線(幾乎每個TextView子類),除非它們具有重力,這就是爲什麼OP的佈局無法正確工作。查看更多這裏:https://possiblemobile.com/2013/10/shifty-baseline-alignment/ – 2017-05-23 22:06:11

相關問題