交换碎片时Android FAB锚更改
我有一个奇怪的问题,当我替换一个片段并使用back stack返回时,当我将其锚定到底部时,FAB有时会显示在屏幕的左上角。当刚从活动中加载时,它完全固定在底部。我怎样才能解决这个问题?提前谢谢 编辑 有50%的几率它锚定正确,50%的几率它不会 我曾尝试使用此方法在java代码中手动设置FAB的重要性,但仍然不起作用交换碎片时Android FAB锚更改,android,Android,我有一个奇怪的问题,当我替换一个片段并使用back stack返回时,当我将其锚定到底部时,FAB有时会显示在屏幕的左上角。当刚从活动中加载时,它完全固定在底部。我怎样才能解决这个问题?提前谢谢 编辑 有50%的几率它锚定正确,50%的几率它不会 我曾尝试使用此方法在java代码中手动设置FAB的重要性,但仍然不起作用 FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab); Coo
FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.anchorGravity = Gravity.BOTTOM | GravityCompat.END;
fab.setLayoutParams(lp);
这是我的晶圆厂代码
<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:orientation="vertical"
tools:context="com.berstek.gradius.fragments.RecordsFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recview_records0"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white"
app:layout_anchor="@id/container"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
我最终使用了框架布局,它显示的很好。但如果有人知道为什么协调人的布局会有这样奇怪的问题,我很想听听 这是我的密码
<FrameLayout 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:background="@android:color/white"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
tools:context="com.berstek.gradius.fragments.RecordsFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recview_records0"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="10dp"
android:clickable="true"
android:src="@drawable/ic_add_white"
app:backgroundTint="@color/colorAccent"
app:layout_anchorGravity="bottom|end" />
一个对我有效的解决方案是在OnResume中调用视图的RequestLayout(具有浮动操作按钮的视图):
@Override
public void onResume() {
super.onResume();
getView().post(new Runnable() {
@Override
public void run() {
getView().requestLayout();
});
}
在Xamarin Android上:
public override void OnResume()
{
base.OnResume();
this.View.Post(() => this.View.RequestLayout());
}
希望有帮助。我只在协调布局中添加了一个字符串,问题消失了
android:fitsSystemWindows="true"
因此,我可以得出结论,有时协调器布局不在全屏上,然后出现这种错误行为
这是我布局的全部代码
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/accountTransactionList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add_white_24dp"
app:layout_anchor="@id/accountTransactionList"
app:layout_anchorGravity="bottom|right|end"
/>
</android.support.design.widget.CoordinatorLayout>
Hi。你找到解决这个问题的办法了吗?