【问题标题】:Android linear layout, how to match remaining parent?Android线性布局,如何匹配剩余父级?
【发布时间】:2014-08-14 04:12:20
【问题描述】:

我要创建一个底部有两个按钮的列表视图

<?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">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        <ListView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:id="@android:id/list"></ListView>
    </LinearLayout>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:text="Add Task"
                android:id="@+id/addTaskButton"></Button>
        <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:text="Add Task2"
                android:id="@+id/addTaskButton2"></Button>
    </LinearLayout>
</LinearLayout>

如何使列表视图与剩余父级的高度匹配?

如果我为列表视图设置高度

            android:layout_height="400dp"

这并不适合所有设备

如果我设置 match_parent

            android:layout_height="match_parent"

列表视图将覆盖底部的按钮

【问题讨论】:

  • 利用android:layout_weight参数,不管屏幕大小,都可以做相应的调整。
  • 然后使用RelativeLayout。在列表视图下方对齐包含按钮的线性布局,并将其对齐在父视图的底部。你有相对布局的参数来实现这一点。
  • 上面的代码没有错..尝试使用 hieght as wrap 运行它
  • 使用RealativeLayout作为父级并赋予按钮布局属性“android:layout_alignParentBottom=true”和listview布局属性“android:layout_above="id of button layout"

标签: android android-layout android-listview android-linearlayout


【解决方案1】:

如果您将其高度设置为0dp 并将其布局权重设置为1,它将填充剩余空间。此外,您不需要将LinearLayout 包围在ListView 周围。

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@android:id/list" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:text="Add Task"
            android:id="@+id/addTaskButton" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="Add Task2"
            android:id="@+id/addTaskButton2" />

    </LinearLayout>

</LinearLayout>

【讨论】:

  • "将其高度设置为 0dp 并将其布局权重设置为 1" 是关键,谢谢,它成功了!!!
【解决方案2】:

这会起作用,而且我还减少了代码中的行数:

 <?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" >

    <ListView
        android:id="@android:id/list"
        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" >

        <Button
            android:id="@+id/addTaskButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:text="Add Task" >
        </Button>

        <Button
            android:id="@+id/addTaskButton2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="Add Task2" >
        </Button>
    </LinearLayout>

</LinearLayout>

【讨论】:

    【解决方案3】:

    使用这个布局

    <?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">
    
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.9" >
    
            <ListView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@android:id/list"></ListView>
        </LinearLayout>
    
    
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp" 
                android:layout_weight="0.1">
    
            <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_weight="1"
                    android:text="Add Task"
                    android:id="@+id/addTaskButton"></Button>
            <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:text="Add Task2"
                    android:id="@+id/addTaskButton2"></Button>
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      你可以使用这个布局:

       <RelativeLayout 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"
           >
      
          <ListView
              android:id="@+id/listview"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_above="@+id/lv_button"
              android:layout_alignParentTop="true"
              android:layout_marginBottom="@dimen/margin_bottom"
               >
          </ListView>
      
          <LinearLayout
              android:id="@+id/lv_button"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_marginBottom="@dimen/margin_bottom"
              android:layout_marginLeft="@dimen/margin_left"
              android:layout_marginRight="@dimen/margin_right"
              android:layout_marginTop="@dimen/margin_top"
              android:orientation="horizontal" >
      
              <Button
                  android:id="@+id/btn1"
                  android:layout_width="0.0dip"
                  android:layout_height="@dimen/button_height"
                  android:layout_marginRight="@dimen/margin_right"
                  android:layout_weight="1.0"           
                  android:text="Button1"
                  />
      
              <Button
                  android:id="@+id/btn2"            
                  android:layout_width="0.0dip"
                  android:layout_height="@dimen/button_height"
                  android:layout_marginLeft="@dimen/margin_left"
                  android:layout_weight="1.0"            
                  android:text="Button2"
                   />
          </LinearLayout>
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        使用重量总和。请参考此link 了解 weightsum

           <?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" android:weightSum="10">
        
           <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="8"
               >
        
            <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                     android:id="@android:id/list"></ListView>
         </LinearLayout>
        
        
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0" 
                            android:layout_weight="2"
              >
        
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_weight="1"
                    android:text="Add Task"
                    android:id="@+id/addTaskButton"></Button>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:text="Add Task2"
                    android:id="@+id/addTaskButton2"></Button>
           </LinearLayout>
         </LinearLayout>
        

        【讨论】:

          【解决方案6】:

          试试这个方法,希望能帮助你解决问题。

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" >
          
                  <ListView
                      android:layout_width="match_parent"
                      android:layout_height="400dp"
                      android:id="@android:id/list"></ListView>
              </LinearLayout>
          
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1">
          
                  <Button
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="left"
                      android:layout_weight="1"
                      android:text="Add Task"
                      android:id="@+id/addTaskButton"></Button>
                  <Button
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="right"
                      android:layout_weight="1"
                      android:text="Add Task2"
                      android:id="@+id/addTaskButton2"></Button>
              </LinearLayout>
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多