我正在尝试构建一个包含有两个LisView对象的LinearLayout的TextView项。第一个对象需要屏幕宽度的70%,而另一个对象需要固定在125 at宽度。
我使用layout_weight属性来告诉布局,以适当地进行第一个TextView缩放,但我无法使它正常工作。它在预览框中看起来很好(屏幕1),但是在模拟器中,左边的TextView总是调整大小以适应它的内容(屏幕2)。
有人知道这是怎么回事吗?我是刚开始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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
android:id="@+id/TitleTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16sp" />
<TextView
android:id="@+id/ValueTextView"
android:layout_below="@id/TitleTextView"
android:layout_width="125dp"
android:layout_height="fill_parent"
android:layout_weight="0"
android:gravity="center_vertical"
android:text="20,000"
android:textSize="16sp" />
</LinearLayout>


发布于 2015-01-17 12:30:23
当提供重量时,宽度(或垂直布局的高度)必须是0 0dp。
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:weightSum="10"
android:padding="6dip">
<TextView
android:id="@+id/TitleTextView"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16sp" />
<TextView
android:id="@+id/ValueTextView"
android:layout_below="@id/TitleTextView"
android:layout_width="125dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="20,000"
android:textSize="16sp" />
</LinearLayout>发布于 2015-01-17 13:01:28
好吧,我已经解决了这个问题。有两件事是不正确的。首先,来自@ answer 3249477的答案是正确的,我需要将weightSum包含在父LinearLayout标记中,并且我还需要将应该缩放的文本元素的宽度设置为0dp。
然而,这仍然没有真正解决我的问题。实际问题是父ListView的宽度和高度设置为"wrap_content“,而不是"fill_parent”。我对此进行了更新,版面开始工作。
我将接受来自@user3249477的答案,因为他是正确的,但我也会感谢它,如果有人可以-投票这个答案,因为它确实允许上述修复工作。
谢谢!
https://stackoverflow.com/questions/27999474
复制相似问题