【发布时间】:2016-05-06 14:24:14
【问题描述】:
如果 FAB(浮动操作按钮)在快餐栏出现之前隐藏(在 CoordinatorLayout 中),那么下次我显示 FAB 时,它会在旧位置绘制(未向下移动到原始位置)。如果在小吃店消失时可以看到 FAB,那么一切都按预期工作。我错过了什么还是一个错误?
UPD:根据要求,这是一个最小示例。我将把最重要的部分放在这里,可以找到一个完整的工作示例on my github
这只是一个来自 Android 模板 Activity 项目的略微修改的示例。
activity_main.xml:
<?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="coordination.fab.snackbar.test.testfabsnackbar.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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/content_main" />
<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="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
还有一个来自我的MainActivity 的OnCreate 方法(扩展AppCompatActivity):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fab.hide();
Snackbar.make(view, "blah", Snackbar.LENGTH_SHORT).show();
new Timer().schedule(new TimerTask() {
@Override
public void run() { fab.show(); }
}, 3000);
}
});
}
基本上,我只是确保在隐藏快餐栏时操作按钮保持隐藏状态。
如果您需要更多信息,请告诉我。
UPD2 如果你偶然发现了这个帖子并且不喜欢它 - 请告诉我我可以改进的地方。我真的很想知道如何解决这个问题。
UPD3 在 Android 错误跟踪器中创建的问题已合并到 this 中,应该会在下一个版本中修复
【问题讨论】:
-
你能告诉我们你隐藏FAB的代码吗?也向我们展示您的布局。
-
@DanielNugent 你去。告诉我是否还有什么不清楚的地方。
-
@gj_ 我已经在我的问题的 UPD 部分中的一个最小示例中说明了布局
标签: android floating-action-button android-snackbar