这个问题在这里已有答案:
我搜索过并发现很多人都报告了这个问题 . 这些是我经历过的一些链接:
-
Android, ListView layout_weight not working, height set to 0dp
-
Android - Linear layout weight not working properly in listview items
-
Android Inside Custom ListView, TextView Weight property not working
还有一些其他人
这些链接提出了以下解决方案
-
替换mInflater.inflate(resID,parent,false);使用mInflater.inflate(resID,null,false);
-
将列表视图设为android:layout_width = "match_parent" android:layout_height = "wrap_content"我甚至尝试过fill_parent,但它没有用 .
-
Listview里面另一个线性布局
-
在相对布局中行xml并使用线性布局
-
对于水平方向,使android:layout_width = "0dp" . 当我这样做时,列表视图不会被弹出 . 它仍然是空白的
我也无法在Android文档中找到任何针对此特定问题的内容 .
我的列表视图:
android:id="@+id/lst_stocks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/row_heading"
android:layout_marginTop="@dimen/margin_5"
我的另一个xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/txt_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/margin_10"
android:text="Rating"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark" />
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxEms="10"
android:padding="@dimen/margin_10"
android:singleLine="true"
android:text="Companay"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
/>
<TextView
android:id="@+id/txt_sector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxEms="10"
android:padding="@dimen/margin_10"
android:singleLine="true"
android:text="Sector"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
我也在其他地方使用这个xml文件,它工作得很好 .
-
所以我错过了什么 . 为什么线性布局权重不起作用?
-
有效地在Listviews中使用LinearLayouts的准则
附:我已经在两个单独的手机上运行文件,而不是在模拟器上运行 .
2 回答
将TextView layout_width设为0dp .
如果你想用重量来设置
TextView
的高度,你应该这样做:同样,如果您要设置宽度,可以使用
width="0dp"
和wieght="1"
.