【问题标题】:Android layout with fixed button on bottomAndroid 布局,底部有固定按钮
【发布时间】:2018-09-02 14:24:33
【问题描述】:

我有以下 xml,它应该类似于 the following

活动显示大部分正常,但我无法在不弄乱顶部视图的情况下将两个向下按钮固定到屏幕底部。

我尝试使用FrameLayout 和两个RelativeLayouts,alignment_bottom,组件之间权重为1 的空视图,甚至仍然无法实现我的目标。任何帮助将不胜感激,在此先感谢您。

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

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

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/RelativeLayout1">

    <ImageView
        android:id="@+id/group_info_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:adjustViewBounds="true"
        android:layout_alignParentTop="true"
        />

    <TextView
        android:id="@+id/description_text"
        android:layout_below="@id/group_info_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/mainColor"
        android:textSize="20sp"
        android:text="@string/descripcion"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:fontFamily="sans-serif"
        />

    <TextView
        android:layout_below="@id/description_text"
        android:gravity="start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/group_info_description"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"

        android:textSize="15sp"/>

    <TextView
        android:layout_below="@id/group_info_description"
        android:scrollbars="vertical"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:layout_marginTop="10dp"
        android:layout_marginStart="5dp"
        android:layout_marginBottom="2dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/group_info_user_number"
        android:textSize="15sp"
        />

    <es.tretornesp.clickerchat.NonScrollableListView
        android:layout_below="@id/group_info_user_number"
        android:id="@+id/group_info_user_list"
        android:layout_height="match_parent"
        android:divider="@drawable/list_divider"
        android:dividerHeight="1px"
        android:layout_width="match_parent"/>

</RelativeLayout>
</ScrollView>

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

    <Button
        android:id="@+id/delete_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/borrar_grupo"
        android:background="@color/mainColor" />

    <View
        android:id="@+id/divisor"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey"/>


    <Button
        android:id="@+id/exit_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="@string/salir"
        android:background="@color/mainColor"
        />
</LinearLayout>
</LinearLayout>

【问题讨论】:

  • 您用模拟器在哪些设备上试过这个?
  • 将按钮放在滚动视图之外,否则它也会滚动..
  • @Kwright02 我的笔记本电脑无法运行模拟器,我通过 adb 在 bq aquaris m5 (api 25) 和 samsung note 8 上进行测试
  • @MohamedMohaideenAH 已编辑,改进不错,但还是有同样的问题
  • 你想要什么?你想删除和退出组按钮与底部对齐还是整个线性布局与底部对齐?

标签: android xml


【解决方案1】:

如下更改布局文件,

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/exit_group">

        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/group_info_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:adjustViewBounds="true"
                android:scaleType="fitStart" />

            <TextView
                android:id="@+id/description_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/group_info_image"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:fontFamily="sans-serif"
                android:text="@string/descripcion"
                android:textColor="@color/mainColor"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/group_info_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/description_text"
                android:layout_marginBottom="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:gravity="start"

                android:textSize="15sp" />

            <TextView
                android:id="@+id/group_info_user_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/group_info_description"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="5dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:scrollbars="vertical"
                android:textColor="@android:color/black"
                android:textSize="15sp" />

            <es.tretornesp.clickerchat.NonScrollableListView
                android:id="@+id/group_info_user_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/group_info_user_number"
                android:divider="@drawable/list_divider"
                android:dividerHeight="1px" />

            <Button
                android:id="@+id/delete_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/group_info_user_list"
                android:background="@color/mainColor"
                android:text="@string/borrar_grupo" />

            <View
                android:id="@+id/divisor"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@id/delete_group"
                android:background="@color/grey" />


        </RelativeLayout>

    </ScrollView>

    <Button
        android:id="@+id/exit_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/mainColor"
        android:text="@string/salir"
        android:textColor="@android:color/white" />


</RelativeLayout>

【讨论】:

  • @Tretorn 请添加您的“编辑”作为附加答案,而不是修改现有答案中的代码。
【解决方案2】:
Use Coordinator Layout as a parent layout. like this.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">


// your code 

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txt_contact_laksha_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:text="demo"
        />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 2019-09-02
    • 2017-10-16
    相关资源
    最近更新 更多