我有一个 TableRow
扩展到整个屏幕的宽度,在那个 TableRow
中我有一个 ListView
其 layout_width
设置为 match_parent
,但它仍然没有扩展到屏幕宽度。它工作正常,但自从我在下一个 TableRow
中引入了 2 个按钮后,问题就出现了。我已将 layout_span
属性设置为 2,但仍然不起作用。这是我的 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/saltnpepper">
<TableRow
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_height="200dp"
android:layout_width="match_parent"
android:background="#FF00FF"
android:layout_span="2"/>
</TableRow>
<TableRow
android:layout_weight="4"
android:layout_marginTop="5dp"
android:background="#FF00FF"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list1"
android:layout_span="2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</ListView>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent">
<Button
android:id="@+id/btnAddToOrder"
android:text="Add To Order"
android:layout_height="wrap_content"
android:layout_width = "match_parent"
android:background="#FF00FF" />
<Button
android:id="@+id/btnProceed"
android:text="Order Now"
android:layout_height="wrap_content"
android:layout_width = "match_parent"
android:background="#FF00FF" />
</TableRow>
</TableLayout>
我已经尝试更改背景颜色,所以我确定确切的问题是 ListView
没有扩展整个屏幕宽度,而 TableRow
(中间一个)非常适合屏幕
最佳答案
试试这段代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00"
android:layout_weight="3">
<TableRow
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_height="200dp"
android:layout_width="match_parent"
android:background="#FF00FF"
android:layout_span="4"/>
</TableRow>
<TableRow
android:layout_weight="2"
android:gravity="center">
<RelativeLayout
android:layout_weight="1"
android:layout_marginTop="5dp"
android:background="#FF00FF"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/list1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
>
</ListView>
</RelativeLayout>
</TableRow>
<TableRow
android:gravity="center"
android:layout_width="match_parent">
<Button android:id="@+id/btnAddToOrder"
android:text="Add To Order"
android:layout_height="wrap_content"
android:layout_width = "match_parent"
android:background="#FF00FF" />
<Button android:id="@+id/btnProceed"
android:text="Order Now"
android:layout_height="wrap_content"
android:layout_width = "match_parent"
android:background="#FF00FF" />
</TableRow>
</TableLayout>
它正在显示您想要的输出。
关于android - 布局不填充父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701655/