【问题标题】:floating action button unexpected anchor gravity change浮动动作按钮意外锚重力变化
【发布时间】:2017-03-15 00:11:33
【问题描述】:

我有一个活动和几个片段。其中一个片段包含FAB,具有锚重力底|右|端。它看起来不错,但是当我开始另一个片段然后按返回按钮时,我的 FAB 出现在左上角。我使用协调器布局,但没有帮助。这是我的布局:

<?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">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/fragment_main_page">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/main_page_listview"
    android:layout_marginTop="5dp">

</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="3dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:src="@drawable/info_main_page"
        android:layout_gravity="center_vertical"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_vertical"
        android:id="@+id/how_to_use"
        android:text="@string/how_to_use"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic"
        android:background="@android:color/transparent"/>

</LinearLayout>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floating_promos"
    android:layout_width="42dp"
    android:layout_height="42dp"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/floating_promos"
    app:layout_anchor="@id/fragment_main_page"
    app:layout_anchorGravity="bottom|right|end"
    app:backgroundTint="#fb9f12"
    app:fabSize="mini"/>
   </android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 为什么不使用 relativeLayout?
  • @Abdenaceur Lichiheb 有什么帮助?

标签: android android-layout android-fragments floating-action-button


【解决方案1】:

我做了一个小的调整,让它在我需要的时候工作,我只是添加了监听器来检查回收器视图是否更新,并对其中的 FAB 进行了更改。 希望这会对你有所帮助。

listWorkData.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        listWorkData.removeOnLayoutChangeListener(this);

        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) btnSubmitConfirm.getLayoutParams();
        lp.anchorGravity = Gravity.BOTTOM | GravityCompat.END;
        btnSubmitConfirm.setLayoutParams(lp);
    }
});

listWorkData.getAdapter().notifyDataSetChanged();

【讨论】:

  • 我有同样的问题,你的解决方案有效。你知道这个bug的原因是什么吗?
  • @VasilyKabunov 我不能说出正确的原因,但我认为问题在于执行时间。 FAB 图标在 UI 中的显示速度应该比导致默认 FAB 参数的回收器视图更快。
猜你喜欢
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 2020-03-19
  • 2015-03-19
  • 2015-03-11
  • 1970-01-01
相关资源
最近更新 更多