[英]Android Floating buttons are not set at a fixed position
I am showing the 2 floating buttons in my activity but the floating buttons are setting at bottom end when there is no data to show in the activity. 我在活动中显示2个浮动按钮,但是当活动中没有数据可显示时,浮动按钮设置在底部。 if records are show in the activity then the floating buttons are showing after the records instead of at a fix position
如果记录显示在活动中,则在记录之后而不是在固定位置显示浮动按钮
Following is my activity layout 以下是我的活动布局
<?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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/login_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@color/login_header"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:src="@drawable/info" />
<ImageView
android:id="@+id/imgLogout"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginStart="290dp"
android:src="@drawable/logout" />
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="10dp"
android:src="@drawable/logo1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:paddingStart="120dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal|center_vertical"
android:background="@color/login_header"
android:src="@drawable/userprofile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/segoeui"
android:paddingStart="20dp"
android:text="@string/name"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/machineLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/search"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/search" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/addNew"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/add" />
Try this 尝试这个
<?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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:src="@drawable/ic_message" />
<ImageView
android:id="@+id/imgLogout"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginStart="290dp"
android:src="@drawable/ic_message" />
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="10dp"
android:src="@drawable/kid_goku" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:paddingStart="120dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@drawable/kid_goku" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="20dp"
android:text="name"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/machineLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/search"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_above="@+id/addNew"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_message" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/addNew"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_message" />
</RelativeLayout>
OUTPUT OUTPUT
Use CoordinatorLayout as root view. 使用CoordinatorLayout作为根视图。 And Also add app:layout_anchorGravity="bottom|right|end(specify as you need) " in FloatingActionButton
并在FloatingActionButton中添加app:layout_anchorGravity =“ bottom | right | end(根据需要指定) ”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.