2014-07-08 112 views
0

您好我已經發現奇怪的行爲,當試圖在android上實現以下佈局。視圖高度計算不正確內加權佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
<LinearLayout 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.25" 
    android:orientation="horizontal" > 
</LinearLayout> 
<LinearLayout 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.75" 
    android:orientation="horizontal" > 
    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dp" 
     android:paddingRight="15dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:layout_margin="20dp" 
     android:textColor="@android:color/black" 
     android:lineSpacingMultiplier="1.1" /> 
</LinearLayout> 

</LinearLayout> 

這塊佈局應該在右側生成一個佔用75%屏幕的textview。但由於某種原因,Textview的高度計算就好像其父代的權重爲0.視圖內部的文本似乎完美包裹,但不是視圖本身。

這是正常的行爲,或者我如何讓textview顯示正確的高度?

回答

0

首先請正確縮進代碼,以便閱讀。您可以通過按Ctrl + Shift + F.

其次,如果你想在LinearLayouts爲25%,寬75%寬,但是填滿屏幕的整個高度,那麼你需要

做自動地在XML編輯器
android:layout_height="match_parent" 

第三,如果您希望TextView佔用屏幕的75%,那麼爲什麼不直接將權重分配給它,而不是將它包裹在另一個LinearLayout中?

最後,配重塊你不需要讓孩子們的權重增加1有

android:layout_weight="1" 

android:layout_weight="3" 

也行。

+0

我不希望它填滿屏幕的高度,我使用wrap_content是有原因的。我已經嘗試在TextView上設置權重無濟於事。我也很清楚這種設置權重的方式,這是我個人的偏好。 –

相關問題