如何声明布局以自动调整多个设备中ListView的高度?

时间:2013-11-28 10:18:18

标签: android android-layout

我正在尝试在设备的按钮中布置按钮。

以下pic1是在4.0英寸设备上布局,pic2在4.7英寸设备上

并尝试了xml代码。

按钮在4.0英寸上排列正确位置, 但不是4.7英寸。

我想自动调整列表视图的高度。

怎么做?


[pic1]

enter image description here


[pic2]

enter image description here


这是xml代码。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:id="@+id/txtLabel2"
            android:text="Buyed items"
            android:textSize="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <ListView
            android:id="@+id/lsv_buyedList"
            android:layout_width="match_parent"
            android:layout_height="450dp">
    </ListView>


    <LinearLayout
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <LinearLayout
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            <Button
                    android:id="@+id/btn_sortById"
                    android:text="SortById"
                    android:textSize="15dp"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"/>

            <Button
                    android:id="@+id/btn_sortByGrade"
                    android:text="SortByGrade"
                    android:textSize="15dp"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"/>
        </LinearLayout>

        <Button
                android:id="@+id/btn_confirm"
                android:text="OK"
                android:textSize="15dp"
                android:layout_width="100dp"
                android:layout_marginRight="10dp"
                android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

4 个答案:

答案 0 :(得分:1)

使用ListView的“权重”字段

android:layout_height="0dip"
android:layout_weight="1"

答案 1 :(得分:1)

android:layout_height="450dp"替换为listView

中的android:layout_height="wrap_conent"

答案 2 :(得分:0)

//从列表视图android:layout_height="450dp"

中删除硬编码值

并使用权重为listview

            <ListView
                android:id="@+id/lsv_buyedList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
        </ListView>

答案 3 :(得分:0)

复制此代码并粘贴它..它可以在我的机器上尝试...享受..!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtLabel2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Buyed items"
    android:textSize="20dp" />

<ListView
    android:id="@+id/lsv_buyedList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/btn_sortById"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="SortById"
            android:textSize="15dp" />

        <Button
            android:id="@+id/btn_sortByGrade"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="SortByGrade"
            android:textSize="15dp" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="OK"
        android:textSize="15dp" />
</LinearLayout>

</LinearLayout>