Android ListView 宽度与父级不匹配

标签 android android-listview android-gridlayout

我的 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/

相关文章:

android - 随着搜索栏的进展,以特定进度触发事件

android - 什么更贵?每次数据更改时创建一个新的 ArrayAdapter 还是更新现有的 ArrayAdapter?

java - 带标题的 Android 多行 ListView

android - 下面带有 MPAndroidChart(或其他)的 ListView

java - 使用网格布局将不同长度的垂直 TextView 与相同的右边缘对齐

java - 创建一个简单的 GridLayout 不起作用

Android:如何临时呈现以编程方式创建的位图以进行调试?

android - admob onclicked 事件

java - SurfaceView同时显示两个动画

android - Gridlayout 不显示,Android 工作室