2015-03-02 51 views
1

這裏是我的代碼layout_above被破壞了嗎?這個簡單的佈局快把我逼瘋了

<TextView 
    android:id="@+id/BIG" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="BIG" 
    android:textSize="50dp" /> 

<TextView 
    android:id="@+id/small" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@id/BIG" 
    android:layout_toRightOf="@id/BIG" 
    android:text="small" 
    android:textSize="10dp" 

    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/small" 
    android:layout_alignLeft="@id/small" 
    android:text="small above" 
    android:textSize="10dp" /> 

這是我得到的結果(實際截圖)。正如你所看到的,整個文本視圖已經消失。

screenshot

這就是我想要的結果(上MSPAINT編輯)

enter image description here

我不能使用,因爲自動填充ALIGN_BOTTOM。這裏是什麼樣子,如果我改用align_Baseline(實際截圖)的ALIGN_BOTTOM已經進行的所有其他垂直對齊後,進行

enter image description here

+0

在第三個文本視圖中添加android:layout_toRightOf =「@ id/BIG」 – 2015-03-02 22:11:04

+0

您的父元素是一個'RelativeLayout',對吧? – tachyonflux 2015-03-02 22:11:07

+0

@karaokyo是的。 – user3453281 2015-03-02 22:14:00

回答

1

顯然基線對齊。
所以「小於」排列在以上「小」當它仍然在其默認位置。然後,「小」與「BIG」的基線對齊,並留在「小於上方」之外,位於RelativeLayout的頂部。

解決這個問題的一個可能的解決方案是將兩個較小的TextView包裝在一個LinearLayout中,然後可以與左側較大的TextView進行正確的基線對齊。
並添加機器人:baselineAlignedChildIndex = 「1」到的LinearLayout,讓第二個孩子的基線對齊到 「大」

參考:http://scottweber.com/2014/02/06/working-with-baselines-in-relativelayout/

+0

謝謝! android:baselineAlignedChildIndex =「1」是拼圖的缺失部分。 – user3453281 2015-03-02 22:39:52

0

試試這個:

<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/BIG" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="BIg" 
    android:textSize="50sp" 
    android:layout_gravity="bottom" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_gravity="bottom"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="small above" 
     android:textSize="10sp" /> 

    <TextView 
     android:id="@+id/small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="small" 
     android:textSize="10sp" /> 


</LinearLayout> 

並使用sp代替文本大小的dp。

+0

我不能使用小寫字母g,這就是爲什麼我必須使用align_Baseline – user3453281 2015-03-03 15:11:20