【问题标题】:Snackbar cover my Button View on Constraint LayoutSnackbar 覆盖我在约束布局上的按钮视图
【发布时间】:2018-10-16 02:39:47
【问题描述】:

我知道默认情况下,只有浮动按钮会在出现野生 Snackbar 时改变其位置。

但如果我们查看this 的问题(非常接近我的问题),我们可以找到解决方案here

基于此,我希望我们有类似或更好的协调器布局解决方案。

发生了什么:

当快餐栏出现时,我的保存按钮被遮住了。 (抱歉,使用 s.png 调整堆栈中的图像大小不起作用)

我想要什么:

当 Snackbar 出现时,我的按钮或整个约束视图会上升

我的代码:

xml代码

<android.support.constraint.ConstraintLayout
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="wrap_content"
android:background="@drawable/background_white_round">


<TextView
    android:id="@+id/dialog_title_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="24dp"
    android:text="@string/str_title"
    android:textColor="@color/solito_text_black"
    android:textSize="@dimen/size_text_title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/dialog_description_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="20dp"
    android:text="@string/str_loading"
    android:textColor="@color/solito_text_grey_dark"
    android:textSize="@dimen/size_text_normal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/dialog_title_text" />

<Button
    android:id="@+id/dialog_ok_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="24dp"
    android:background="@color/background_transparent"
    android:text="@android:string/ok"
    android:textColor="@color/solito_button_purple"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/dialog_description_text"/>

<Button
    android:id="@+id/dialog_cancel_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="24dp"
    android:background="@color/background_transparent"
    android:text="@string/str_cancel"
    android:textColor="@color/solito_button_purple"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/dialog_ok_btn"
    app:layout_constraintTop_toBottomOf="@id/dialog_description_text"/>
</android.support.constraint.ConstraintLayout>

java代码

private void showSnackBarUndo() {
    //Snack Bar to Undo
    Snackbar snackbar = Snackbar
            .make(Objects.requireNonNull(getView()), getResources().getString(R.string.str_list_cleaned), Snackbar.LENGTH_LONG)
            .setAction(getResources().getString(R.string.str_undo), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DataStorePersistence.getInstance().setAddressArrayListTemp(getContext(), listTitle, listID, addressArrayListUndo);
                    updateListContent();
                    Snackbar
                            .make(getView(),
                                        getResources().getString(R.string.str_list_restored),
                                    Snackbar.LENGTH_SHORT)
                            .show();
                }
            });
    snackbar.show();
}

编辑: 我尝试了很多东西。示例尝试将行为放在一个自定义类按钮中,但没有奏效。学习 Material Design Android 看起来他们不想让你把按钮放在屏幕底部。所以到目前为止,我正在更改为浮动按钮。不是问题的解决方案,但也许可以帮助到达这里的人。

【问题讨论】:

  • 将你的布局包裹在 CoordinatorLayout 中,并在 Snackbar 对象中传递 Coordinator Layout id。Snackbar.make(R.id.cordLayout)。那么它应该可以正常工作了。
  • @PonsuyambuVelladurai 没用 =/

标签: android xml android-layout android-snackbar


【解决方案1】:

如果您尝试的所有方法都不起作用。然后在每次显示和关闭小吃栏时尝试增加和减少保存按钮的底部边距 即

...
snackbar.addCallback(new Snackbar.Callback() {

    @Override
    public void onDismissed(Snackbar snackbar, int event) {
      LinearLayout.LayoutParams param = theSaveButton.getLayoutParams();
       param.bottomMargin = 10 ; //or the previous bottom margin
       theSaveButton.setLayoutParams(param);
    }

    @Override
    public void onShown(Snackbar snackbar) {
       LinearLayout.LayoutParams param = theSaveButton.getLayoutParams();
       param.bottomMargin = 80 ; //80dp being the height of snackbar
       theSaveButton.setLayoutParams(param);
    }
  });
snackbar.show();
...

或者更糟的是从小吃栏监听器中更改按钮的位置

【讨论】:

  • 我的 Button 是 ViewGroup.LayoutParams 而不是 LinearLayout,ViewGroup 不允许我使用 bottomMargin。甚至尝试使用 ConstraintLayout 但没有用
【解决方案2】:

可能最简单的做法是获取FloatingActionBar.Behavior 的源代码(如果有任何代码需要视图类型为FloatingActionButton,则很少)并对其进行修改,使其与常规Button 一起使用.然后只需在布局中将新的自定义 Behavior 类的实例设置为 Button

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.your.app.ButtonBehavior" />

【讨论】:

  • 尝试放置 Coordinator Layout 而没有,仅使用行为,没有工作
  • OK - 可能获取源代码并根据您的需要进行修改。更新了我的答案。
  • 只有app:layout_behavior="com.your.app.ButtonBehavior"没用=/
  • 您获取了现有的源代码,创建了自己的自定义行为,将其更新为在 Button 上工作,将其设置在 XML 中的 Button 上,并将 Button 放入 @ 987654331@,还是不行?
  • 是的,也许我做错了什么,但应该很容易复制、粘贴并将浮动更改为按钮。但是没有用。我使用了正确的 com.MyApp... 路径。 @dominicoder
【解决方案3】:

不确定我的 SnackProgressBar 库是否会有所帮助。您首先创建一个SnackProgressBarManager,然后将该按钮作为视图的一部分包含在与snackBar 一起移动的视图中。

// always have one instance of SnackProgressBarManager only in the activity
snackProgressBarManager = new SnackProgressBarManager(view)
        // (optional) set the view which will animate with SnackProgressBar e.g. FAB when CoordinatorLayout is not used
        .setViewToMove(buttonView)

完整的文档可在 Github 上找到。如果您也想将它用于其他目的,它非常灵活。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2021-12-26
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多