Android ListView宽度与父级不匹配

时间:2014-06-21 18:40:14

标签: android android-listview android-gridlayout

大家好! 我的Android应用程序标记有问题。我将GreedLayout里面的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>

1 个答案:

答案 0 :(得分: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>

我希望这能解决你的问题吗?

相关问题