2013-11-09 48 views
0

我在LinearLayout中有兩個TextView。佈局方向處於水平狀態。我必須保持兩個Textview高度相等。但根據文本長度,兩者都顯示在不同的高度。在LinearLayout中維護textview高度

我曾試圖做出同樣的文本長度如下面

public static String padRight(String s, int n) { 

return String.format("%1$-" + n + "s", s); 

} 

但高度略有不同。我如何解決這個問題?

+0

您可以修復的TextView的一些恆定值layout_height參數, wrap_content – AsG

+0

你可以在這裏發佈截圖,請 –

+0

給文本視圖提供換行內容,這將具有較高的高度,併爲另一個文本視圖提供匹配父項並將換行內容提供給線性佈局它也會工作,但我認爲不是一個完美的解決方案。 –

回答

1

如果我沒有誤解你,你想TextView s在一個水平面向LinearLayout,並且兩個TextView s必須在相同的高度。

然後使用一個佈局類似如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#B40431" 
     android:text="text sample\ntext sample\ntext sample\ntext sample\ntext sample\ntext sample\n" 
     android:gravity="center_vertical"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#0000FF" 
     android:text="text sample\ntext sample\ntext sample" 
     android:gravity="center_vertical"/> 

</LinearLayout> 

輸出將是這樣的:不是

enter image description here

+0

感謝您的回覆。我已修復該問題。 –

1

因爲等你們設置的單行您的TextView的真,那麼它會工作文本較長的TextView挺大,

在你的TextView XML添加此行: -

android:singleLine="true" 

希望它有助於你..

+0

感謝您的回覆 –

+0

請接受&upvote的答案,如果它有助於解決您的問題 –

+0

如果我使用單行,沒有必要考慮關於TextView Heigth。你已經回答了我的問題。所以我發送了感謝信息。愛琴海的答案是確切的答案。但在看到他回答之前,我已經解決了這個問題。但答案是正確的。所以我已經接受了他的回答。接受和upvote是沒有關係的.... –