В настоящее время у меня проблема с видом переработчика и плавающей кнопкой действия в моем приложении для Android. Кнопка всегда переходит в правую верхнюю часть страницы, когда я запускаю приложение.
RecyclerView и кнопка находятся внутри фрагмента. В настоящее время я использую ConstraintLayout, чтобы сказать, что кнопка всегда должна появляться в правом конце экрана. Тем не менее, я также попробовал это с RelativeLayout и CoordinatorLayout с атрибутами привязки и гравитации. В Android Studio макет показывает это правильно, с кнопкой в нижней правой части экрана. Как только я запускаю приложение на своем телефоне, оно появляется в верхней правой части экрана...
Предварительный просмотр макета:
Когда я запускаю приложение на своем телефоне:
Вот мой код, который я сейчас использую для фрагмента:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_bucketlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_bucketList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_addToBucketList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
Заранее спасибо за помощь!
Я решил проблему с помощью RelativeLayout. Возникла проблема с тем, что кнопка "Плавающее действие" всегда отображалась в нижней части экрана и "зависала" в нижней панели навигации (устройства HUAWEI). Атрибут app: useCompatPadding = "true" решил проблему.
Вот обновленный код:
<?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"
android:id="@+id/fragment_bucketlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_bucketList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_addToBucketList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit"
app:useCompatPadding="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Попробуйте использовать окно повторного просмотра и потрясающую кнопку в CoordinatorLayout.
Атрибут app:layout_anchorGravity:
будет указывать, как должен располагаться объект относительно якоря по осям X и Y в пределах родительских границ.
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_bucketlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_bucketList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:scaleType="center"
android:src="@drawable/android_floating_action_button_icon"
app:layout_anchor="@id/rv_bucketList"
app:layout_anchorGravity="bottom|right|end"
app:borderWidth="1dp"
app:elevation="10dp" />
</android.support.design.widget.CoordinatorLayout>
Попробуй это. Измените constrainLayout наlativeLayout
<RelativeLayout android:id="@+id/fragment_bucketlist"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_bucketList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_addToBucketList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="50dp"
android:layout_alignParentEnd="true"
app:fabSize="normal"/>
</RelativeLayout>