【发布时间】: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