【问题标题】:Cant arrange LinearLayout to show button at the bottom with ListView above无法安排 LinearLayout 以在底部显示按钮,上面的 ListView
【发布时间】:2017-09-13 19:42:04
【问题描述】:

考虑下一段 xml -

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

    <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"
             tools:context="com.android.fiby.fragments.MenuFragment"
             android:orientation="vertical"
             android:layout_weight="0.7">

        <com.android.fiby.TitleFont
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#5e240a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="40dp"
            android:text="בדיקה"
            android:id="@+id/list_title"/>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:visibility="visible"
              android:layout_gravity="bottom"
              android:gravity="center"
                  android:layout_weight="0.05">

        <ImageView
            android:id="@+id/buy_now"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/button"/>

    </LinearLayout>

</LinearLayout>

这就是它的外观(忽略 FAB 和操作按钮)

问题是当我从列表视图中删除元素时,按钮也会上升。我希望这样,无论 ListView 中有多少项,按钮总是在底部。

编辑

我尝试过以下方式使用相对布局 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

    <com.android.fiby.TitleFont
        android:textColor="#5e240a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="40dp"
        android:text="בדיקה"
        android:id="@+id/list_title"
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true"/>



    <ListView
        android:layout_below="@+id/list_title"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <ImageView
        android:layout_below="@android:id/list"
        android:id="@+id/buy_now"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

但问题是列表视图覆盖了按钮-

我不想更改列表的高度,因为其他活动会禁用图像可见性。

【问题讨论】:

    标签: android xml listview


    【解决方案1】:

    您可以使用 RelativeLayout 而不是 LinearLayout 并将此按钮始终设置在列表视图下方。像这样的:

    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:visibility="visible"
              android:layout_gravity="bottom"
              android:gravity="center"
              android:id="@+id/listview"
              android:layout_weight="0.05">
    
        <ImageView
            android:id="@+id/buy_now"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listview"
            android:src="@drawable/button"/>
    

    或者,如果您希望您的按钮始终位于活动的底部,请尝试以下操作:

            android:layout_alignParentBottom="true"
    

    编辑1: 尝试将 ListView 设置在按钮上方,而不是在 listview 下方设置按钮。

    <ListView
        android:layout_below="@+id/list_title"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/buy_now"/>
    
    <Button
        android:id="@+id/buy_now"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"/>
    

    或者你可以使用 ScrollView...

    【讨论】:

      【解决方案2】:

      你应该尝试使用RelativeLayout并使用这一行

      android:layout_alignParentBottom="true"
      

      这会将项目停靠在底部

      【讨论】:

        猜你喜欢
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-03
        • 1970-01-01
        • 2013-01-24
        • 2020-11-09
        • 1970-01-01
        相关资源
        最近更新 更多