简体   繁体   中英

Cannot position floating action button to bottom right of the screen

For a strange reason my floating action button wont go down to the lower right portion of my android activity which is suppose to be its default behavior.

What did I do wrong in my layout.xml ?

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_menu_myplaces" />

BELOW IS MY WHOLE LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="100dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
    tools:context=".MainActivity"
    android:elevation=".5dp"
    android:background="@drawable/trizbg"
    android:focusable="true">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:src="@drawable/h_biglogo"
        android:paddingTop="8.5dp"
        android:paddingBottom="8.5dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/hiaTextField"
        android:layout_alignEnd="@+id/hiaTextField"
        android:layout_above="@+id/hiaTextField" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/hiaTextField"
        android:selectAllOnFocus="true"
        android:textColorHint="#ffffffff"
        android:hint="Tell us where you're going"
        android:drawableLeft="@android:drawable/ic_search_category_default"
        android:text=""
        android:autoText="false"

        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SEARCH"
        android:id="@+id/hiaButton"
        android:onClick="hiaSubmitClick"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"

        android:layout_below="@+id/hiaTextField"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="18sp"
        android:textColor="@color/colorWhite"

        android:alpha="20"
        android:layout_alignRight="@+id/hiaTextField"
        android:layout_alignEnd="@+id/hiaTextField"    
        android:background="@drawable/buttonround"/>   

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_menu_myplaces" />
</RelativeLayout>

Below is the screenshot 在此处输入图片说明

Try to add this attribute for floating action button

          app:layout_anchor="@id/layout" //id of the relativelayout
          app:layout_anchorGravity="bottom|right|end"

General template for getting the FAB at the bottom right is to include your layout inside of a CoordinatorLayout , then have the button to aligned to the buttom-right of that.

I think your problem is that the layout_gravity attribute doesn't apply to RelativeLayout children.

You could do this, but that isn't really the documented way it should be done.

android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" 
android:layout_alignParentEnd="true"

What you should have is like so, of course, replacing that include line with the name of your layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        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"
        android:fitsSystemWindows="true"
        tools:context="com.androidstack.app.NavDrawerActivity2">

    <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <include layout="@layout/your_layout"/>

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_menu_myplaces"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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