如何使用 Android snackbar 弹出整个片段布局
How to pop up whole fragment layout with Android snackbar
我没有任何浮动按钮,但我想在显示小吃栏消息时弹出整个布局。为此,我将片段包装到 CoordinatorLayout 中,如下所示:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container_for_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</android.support.design.widget.CoordinatorLayout>
container_for_fragments
托管我的片段。我声明 CoordinatorLayout
然后像这样使用它:
Snackbar snackbar = Snackbar.make(myCoordinatorLayout, "Hello", Snackbar.LENGTH_LONG);
snackbar.show();
我得到:
有没有办法在出现 snackbar 时弹出整个片段?
写下你自己的行为,对显示做出反应 Snackbar
:
public class FrameLayoutBehavior extends CoordinatorLayout.Behavior<FrameLayout> {
private float mFrTranslationY;
public FrameLayoutBehavior (Context context, AttributeSet attrs) {
super();
}
@Override
public boolean layoutDependsOn(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
if (dependency instanceof Snackbar.SnackbarLayout) {
updateFabTranslationForSnackbar(parent, child, dependency);
ViewCompat.setTranslationY(child, mFrTranslationY);
}
return false;
}
@Override
public void onDependentViewRemoved(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
super.onDependentViewRemoved(parent, child, dependency);
}
private void updateFabTranslationForSnackbar(CoordinatorLayout parent, final FrameLayout fab, View snackbar) {
mFrTranslationY = getFabTranslationYForSnackbar(parent, fab, snackbar);
}
private float getFabTranslationYForSnackbar(CoordinatorLayout parent, FrameLayout fab, View snackbar) {
float minOffset = 0;
minOffset = Math.min(minOffset, ViewCompat.getTranslationY(snackbar) - snackbar.getHeight());
return minOffset;
}
}
然后将参数 app:layout_behavior
添加到您的 FrameLayout
。当 FrameLayout
被 Fragment
替换时,行为可能会丢失。然后你必须添加:
@CoordinatorLayout.DefaultBehavior(FrameLayoutBehavior.Behavior.class)
就在您片段的 class 声明上方。
我没有任何浮动按钮,但我想在显示小吃栏消息时弹出整个布局。为此,我将片段包装到 CoordinatorLayout 中,如下所示:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container_for_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</android.support.design.widget.CoordinatorLayout>
container_for_fragments
托管我的片段。我声明 CoordinatorLayout
然后像这样使用它:
Snackbar snackbar = Snackbar.make(myCoordinatorLayout, "Hello", Snackbar.LENGTH_LONG);
snackbar.show();
我得到:
有没有办法在出现 snackbar 时弹出整个片段?
写下你自己的行为,对显示做出反应 Snackbar
:
public class FrameLayoutBehavior extends CoordinatorLayout.Behavior<FrameLayout> {
private float mFrTranslationY;
public FrameLayoutBehavior (Context context, AttributeSet attrs) {
super();
}
@Override
public boolean layoutDependsOn(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
if (dependency instanceof Snackbar.SnackbarLayout) {
updateFabTranslationForSnackbar(parent, child, dependency);
ViewCompat.setTranslationY(child, mFrTranslationY);
}
return false;
}
@Override
public void onDependentViewRemoved(final CoordinatorLayout parent, final FrameLayout child, final View dependency) {
super.onDependentViewRemoved(parent, child, dependency);
}
private void updateFabTranslationForSnackbar(CoordinatorLayout parent, final FrameLayout fab, View snackbar) {
mFrTranslationY = getFabTranslationYForSnackbar(parent, fab, snackbar);
}
private float getFabTranslationYForSnackbar(CoordinatorLayout parent, FrameLayout fab, View snackbar) {
float minOffset = 0;
minOffset = Math.min(minOffset, ViewCompat.getTranslationY(snackbar) - snackbar.getHeight());
return minOffset;
}
}
然后将参数 app:layout_behavior
添加到您的 FrameLayout
。当 FrameLayout
被 Fragment
替换时,行为可能会丢失。然后你必须添加:
@CoordinatorLayout.DefaultBehavior(FrameLayoutBehavior.Behavior.class)
就在您片段的 class 声明上方。