简体   繁体   English

Android图层列表影响浮动操作按钮的位置

[英]Android layer-list affecting position of floating action button

I am creating an app where a layer-list is used as background and a floating action button is also used. 我正在创建一个应用程序,其中的图层列表用作背景,还使用了浮动操作按钮。 When the layer-list is set as the background of the relative layout, it changes the position of the FAB, but I cannot understand why it happens. 将图层列表设置为相对布局的背景时,它会更改FAB的位置,但我不明白为什么会发生它。

I want the FAB to be positioned at the bottom left. 我希望将FAB放在左下角。 When I don't have the background set as my layer-list, it appears where I have coded. 当我没有将背景设置为图层列表时,它将显示在我编码的位置。

没有背景的FAB添加到相对布局

But when I add the background attribute to the Relative layout, FAB automatically moves in. 但是,当我将背景属性添加到“相对”布局时,FAB会自动移入。

将背景添加到相对布局时的FAB

I can't understand what is affecting this behavior of FAB. 我不明白是什么影响了FAB的这种行为。

XML for layer_list: layer_list的XML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary" />
        <padding
            android:bottom="64dp"
            android:left="32dp"
            android:right="32dp"
            android:top="64dp" />
    </shape>
</item>

<item>
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <corners
            android:bottomLeftRadius="64dp"
            android:bottomRightRadius="64dp"
            android:topLeftRadius="64dp"
            android:topRightRadius="64dp" />
    </shape>
</item>

XML for Activity: 用于活动的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/home_layer_list">

<TextView
    android:id="@+id/text_view"
    android:layout_centerInParent="true"
    android:textSize="36sp"
    android:textStyle="bold"
    android:textColor="@color/colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world" />

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_fab"
    android:layout_margin="16dp"
    app:backgroundTint="#FFFFFF"/>

How can I make the FAB move back to the position where it was initially? 如何使FAB返回到最初的位置?

Try to change your layout like below: 尝试如下更改布局:

<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="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/home_layer_list">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="hello world"
            android:textColor="@color/colorPrimary"
            android:textSize="36sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:src="@drawable/ic_fab"
        android:layout_margin="16dp"
        app:backgroundTint="#FFFFFF" />

</RelativeLayout>

在此处输入图片说明

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

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