2016-11-21 29 views

回答

2

是的,這可以通過使用RelativeLayout輕鬆實現。

您可以在第二個TextView上指定alignEnd屬性並傳遞第一個TextView的ID。此外,您必須指定layoutBelow屬性並再次傳入第一個TextView的ID。

此外,如果第一個TextView水平居中,則可以將centerHorizontal屬性設置爲true。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text_view_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
    /> 

    <TextView 
     android:id="@+id/text_view_2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignEnd="@+id/text_view_1" 
     android:layout_below="@+id/text_view_1" 
    /> 
</RelativeLayout> 
+0

我想他的意思,他希望第二個文本在中心啓動的第一個文本。在您的解決方案中,它只會與第一個文本的末尾對齊。 – fbwnd

+0

是的,萬一它是你建議的方式,那麼我的解決方案將無法正常工作。但他還沒有澄清任何事情。讓我們來看看。 :) –

+0

我需要對齊的第一個文本視圖即時工作在這個現在 –

0

編輯 - 顯然我不明白這個問題。下面的答案將幫助您將第二個文本從第一個文本的中心開始對齊。

您應該使用RelativeLayout。

你可以嘗試這樣的事情:

<RelativeLayout ...> 

    <TextView 
     android:id="@+id/text_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     .../> 

    <View 
     android:id="@+id/shadowView" 
     android:layout_width="1dp" 
     android:layout_height="1dp" 
     android:layout_centerHorizontal="true"/> 

    <TextView 
     android:id="@+id/text_2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toEndOf="@+id/shadowView" 
     android:layout_below="@+id/text_1" 
     .../> 

</RelativeLayout> 

如果您同時支持API 17舊版本,使用toRightOf代替toEndOf

相關問題