繁体   English   中英

如何在相对布局内向父底部对齐按钮添加底部边距?

[英]How to add a bottom margin to a parent bottom aligned button inside a relative layout?

我目前正在使用 android 工作室创建应用程序,但在屏幕右下角放置按钮时遇到问题。 我选择使用一些组件填充相对布局,并且到目前为止, android:layout_marginRight="<x>dp"已经使我能够阻止视图接触边缘。 我成功使用它的示例如下所示(用于悬停在右上角的按钮的 XML):

<RelativeLayout blah>
    <com.google.android.material.button.MaterialButton
        android:id="someID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:align_parentEnd="true" 
        android:layout_marginEnd="20dp"
        android:layout_marginTop="20dp"
        and so on describing the button but no more geometric statements
    />
</RelativeLayout>

However, when I do the same for a button that I add the line android:layout_alignParentBottom and the line android:layout_marginBottom the bottom margin line is not effective and the button is glued to the bottom of the screen - here is the offender: XML of android 工作室渲染旁边的按钮

请有人可以解释为什么会发生这种情况以及如何解决它? 我试图在右下角放置一个按钮,其右侧和下方为 20dp,因此它“悬停”在那里。

这是我的 XML 的 rest 目前的样子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_add_review"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:paddingBottom="20dp"
        android:clickable="true"       
        app:backgroundTint="@color/themeColorOrange"
        app:srcCompat="@drawable/ic_baseline_add_24" />
    
</RelativeLayout>

您可以简单地在您的FloatingActionButton周围放置一个RelativeLayout ,并给它一个20dppadding ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:paddingBottom="20dp"
        android:layout_height="wrap_content">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/btn_add_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="20dp"
            android:clickable="true"
            app:backgroundTint="@color/themeColorOrange"
            app:srcCompat="@drawable/ic_baseline_add_24" />
    </RelativeLayout>
</RelativeLayout>

不幸的是, marginBottom不能与alignParentBottom结合使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM