2014-07-08 64 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上设置权重无济于事。我也很清楚这种设置权重的方式,这是我个人的偏好。 –

相关问题