2013-08-27 37 views
0

我在安排LinearLayout中的項目時遇到問題。 這正是我需要的: enter image description hereLinearLayout安排項目

但是我的代碼:

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:layout_marginRight="4dp" 
       android:text="Hồ-Hoài-Anh"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:layout_marginRight="4dp" 
       android:text="Bàn-thắng"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:text="Hà-Nội-mùa-thu-tháng-8"/> 
     </LinearLayout> 

這是我的結果:

enter image description here

如何將這些項目安排automaticially取決於他們的寬度是多少?

感謝您的關注!

回答

1

您正在將所有三個textviews添加到單個LinearLayout中,其中有horizontal orientation。這就是爲什麼你沒有得到所需的視圖。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:layout_marginRight="4dp" 
       android:text="Hồ-Hoài-Anh"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:layout_marginRight="4dp" 
       android:text="Bàn-thắng"/> 

     </LinearLayout> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/new_hot_hastag" 
       android:text="Hà-Nội-mùa-thu-tháng-8"/> 

</LinearLayout> 
+0

對不起,因爲一個令人困惑的問題。但看看這些「這些項目如何自動安排取決於他們的寬度?」。這意味着:如果3 textview的值是「a」「b」「c」,那麼3個textview由於其寬度小而仍然在1行上。 – EastBK

+0

您正在使用「wrap_content」作爲單個文本視圖的寬度,因此textview將根據文本的寬度佔用寬度。所以,如果你能告訴我你是否想將所有文本視圖都放在一行中,這對我來說會更好。 –

+0

如果3個textview的字符數很少,那麼它們將在1行上。如果兩個第一個textview的字符數很少,而最後一個textview很大,那麼第一個textview將在第一行,第三個textview將在第二個行。它可用於實施? – EastBK

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/one_tv" 
    style="@style/new_hot_hastag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="40dp" 
    android:text="Hồ-Hoài-Anh" /> 

<TextView 
    android:id="@+id/two_tv" 
    style="@style/new_hot_hastag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/one_tv" 
    android:text="Bàn-thắng" /> 

<TextView 
    android:id="@+id/three_tv" 
    style="@style/new_hot_hastag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/one_tv" 
    android:text="Hà-Nội-mùa-thu-tháng-8" /> 

PS的android:文本= 「禁塘」 應該是這樣的:機器人:文本= 「@字符串/兩」

串.xml: Bàn-thắng 祝你好運!

+0

對不起,因爲一個令人困惑的問題。但看看這些「這些項目如何自動安排取決於他們的寬度?」。這意味着:如果3 textview的值是「a」「b」「c」,那麼3個textview由於其寬度小而仍然在1行上。 – EastBK

+0

不可能。 id three_tv是:android:layout_below =「@ id/one_tv」這意味着:沒有任何3 textview的值,「Hà-Nội-mùa-thu-tháng-8」在「Hồ-Hoài-Anh」之下 – AlexChu

+0

自己定義了(id three_tv是:android:layout_below =「@ id/one_tv」),但我沒有。這取決於他們的角色的寬度。 – EastBK