gpt4 book ai didi

Android ListView 宽度与父级不匹配

转载 作者:行者123 更新时间:2023-11-30 02:50:09 25 4
gpt4 key购买 nike

我的 Android 应用程序标记有问题。我将 GridLayout 内 ListView 的宽度设置为 match_parent 并认为它会进入屏幕大小。但是在我的实现中,listView 宽度匹配整个屏幕大小,并且由于左列不适合屏幕。

我尝试使用 TableLayout?但结果是一样的:ListView 宽度比我想要的大,并且超出了屏幕。

我应该更改什么来设置 ListView 宽度以适应屏幕大小。波纹管是我的简化标记。当前结果图片:https://yadi.sk/d/qh9qOVfmUNqEo (这是我的第一个问题,没有足够的声誉在问题中添加图像)。

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.UploadToServerActivity"
android:id="@+id/gridLayoutId"
android:columnCount='2'>

<Button
android:layout_width="185dp"
android:layout_height="70dp"
android:text="@string/select_image"
android:id="@+id/selectPhoto"
android:layout_row="0"
android:layout_column="0"/>


<ListView
android:id="@+id/listView"
android:divider="#b5b5b5"
android:background="#C8C8C8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"/>
</GridLayout>

最佳答案

这是您要构建的布局,借助水平方向的线性布局并使用它的 android:weightSum 属性制作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">

<Button
android:id="@+id/selectPhoto"
android:layout_width="185dp"
android:layout_height="70dp"
android:text="Select Image"
android:layout_weight="55"/>

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#C8C8C8"
android:divider="#b5b5b5"
android:layout_weight="45" />

</LinearLayout>

希望这能解决您的问题?

关于Android ListView 宽度与父级不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24344691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com