Android ListView宽度与父项不匹配

问题描述 投票:1回答:1

我的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 android-listview android-gridlayout
1个回答
1
投票

这里是您要构建的布局,它是借助水平方向的线性布局并使用其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>

我希望这能解决您的问题?

© www.soinside.com 2019 - 2024. All rights reserved.