2016-01-27 38 views
2

我試圖並排放置兩個TextView,並且我想要一個觸摸屏幕右側和另一個左側。我不想用數字定義寬度,因爲不同尺寸的屏幕會有不同的表現。所以我試圖使用layout_gravity,由於某種原因,它不工作。兩個並排TextView的重力

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="16dp" 
    android:layout_gravity="left" 
    android:text="rrr" 
    android:textColor="@color/secondTextColor" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:textSize="16dp" 
    android:text="sss" 
    android:textColor="@color/secondTextColor" /> 

</LinearLayout> 

有誰能告訴我爲什麼?謝謝!

+0

是這兩個文本瀏覽應該在彼此之間觸摸?或者1只接觸左邊,另一個右邊,他們不接觸?如果他們之間有碰觸,那麼'weight'就足夠了,你不需要'weightSum' –

回答

2

您可以爲每一個TextViewLinearLayout如下:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="start"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="16dp" 
     android:text="rrr" 
     android:textColor="#f2f2" 
     /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="end"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16dp" 
      android:text="sss" 
      android:textColor="#f3f3" /> 
    </LinearLayout> 
</LinearLayout> 

而且重要的是,在你的第一個LinearLayout你把android:gravity="start"並在第二個android:gravity="end",那麼它會工作:)

使用end而不是right確保從右到左語言環境中的正確行爲。

爲什麼「結束」比「正確」更好?

使用Gravity#LEFTGravity#RIGHT可導致當一佈局在文本流從rightleft語言環境呈現問題。 改爲使用Gravity#STARTGravity#END。同樣,在XML gravity和layout_gravity屬性中,使用start而不是left。 對於XML屬性,如paddingLeftlayout_marginLeft,請使用paddingStartlayout_marginStart。 注意:如果您的minSdkVersion小於17,則應添加舊的left/right屬性以及新的start/right屬性。在較舊的平臺上,RTL不受支持且start/right屬性未知並因此被忽略,因此您需要較早的left/right屬性。有一個單獨的皮棉檢查,捕捉這種類型的錯誤。

(注:Gravity#LEFTGravity#START,您可以針對老年平臺的時候,因爲起步位掩碼是左位掩碼的一個超集,即使使用這些常量因此,您可以使用gravity="start",而不是gravity="left|start"。)

+1

謝謝!它雖然沒有2個線性佈局工作。爲什麼「終結」比「正確」更好? –

+0

@JoãoMarcos看到我的編輯回答你的問題 –

+0

我明白了,非常感謝! –

3

您可以android:layout_weight & android:gravity嘗試。

閱讀What does android:layout_weight mean & Layout Weight

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

<TextView 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="left" 
    android:text="Intellij" /> 

<TextView 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="right" // You can add end instead of right 
    android:text="Amiya" /> 

</LinearLayout> 
+1

使用'end'而不是'right'來確保從右到左的語言環境中的正確行爲,而'left'也是如此。 –

+0

@Skizo好吧好的。更新。謝謝 。 –

+1

我不知道weightSum屬性。謝謝! –

0

你可以在TextView's0dp使用android:layout_weight="1"width

+0

是不是不像典雅?爲什麼這個工作? –

+0

你爲什麼認爲它不夠優雅?這裏有一個很好的解釋如何在佈局文件中使用權重的教程:[鏈接](https://www.youtube.com/watch?v=rMksRBvYG28) –