如何使用BottomSheetDialog? [英] How to use BottomSheetDialog?
问题描述
我想尝试在Android支持库 23.2 中引入的 BottomSheetDialog ,但它似乎无法正常工作.这是医生说的:
I want to try BottomSheetDialog introduced in Android Support Library 23.2 but it doesn't seem to work correctly. Here is what the doc says:
虽然BottomSheetBehavior捕获了持久的底部工作表案例,但此发行版还提供了BottomSheetDialog和 BottomSheetDialogFragment填充模式底部用例. 只需将AppCompatDialog或AppCompatDialogFragment替换为其 底页等效项,以将对话框样式设置为底部 工作表."
While BottomSheetBehavior captures the persistent bottom sheet case, this release also provides a BottomSheetDialog and BottomSheetDialogFragment to fill the modal bottom sheets use case. Simply replace AppCompatDialog or AppCompatDialogFragment with their bottom sheet equivalents to have your dialog styled as a bottom sheet."
所以我将我的AppCompatDialog
更改为BottomSheetDialog
:
package my.package.ui.dialog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import my.package.R;
public class AccountActionsDialog extends BottomSheetDialog {
public AccountActionsDialog(Context context) {
super(context);
if (context instanceof Activity) {
setOwnerActivity((Activity) context);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutInflater().inflate(R.layout.dialog_account_actions, null));
}
}
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:padding="16dp"
android:text="Delete account"
android:textColor="#ffffff" />
</LinearLayout>
然后我在活动"中使用以下代码:
Then I use the following code in my Activity:
new AccountActionsDialog(this).show();
我的屏幕变暗,但是对话框的内容不可见.对可能缺少的东西有什么想法吗?当我改用 AppCompatDialog 时,效果很好.
My screen becomes dimmed but the content of my dialog is not visible. Any thoughts on what might be missing? It works fine when I use AppCompatDialog instead.
推荐答案
这是BottomSheetDialog的布局文件.
This is the layout file of BottomSheetDialog.
<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"
android:soundEffectsEnabled="false">
<FrameLayout
android:id="@+id/design_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:layout_behavior="@string/bottom_sheet_behavior"
style="?attr/bottomSheetStyle"/>
</android.support.design.widget.CoordinatorLayout>
您的内容视图在视图design_bottom_sheet
内,它将以CoordinatorLayout
垂直居中,而BottomSheetBehavior
会将其偏移.
Your content view is inside the view design_bottom_sheet
, it will be positioned center vertically by CoordinatorLayout
, and BottomSheetBehavior
will offset it.
mParentHeight = parent.getHeight();
mMinOffset = Math.max(0, mParentHeight - child.getHeight());
mMaxOffset = mParentHeight - mPeekHeight;
if (mState == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, mMinOffset);
} else if (mHideable && mState == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, mParentHeight);
} else if (mState == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, mMaxOffset);
}
其意图是在mMaxOffset
上定位在design_bottom_sheet
上,但是实际上子视图的初始getTop不是0,而是(mParentHeight - childHeight) / 2
,因此您查看偏移量是否大于所需的偏移量.
It intented to positon design_bottom_sheet
at mMaxOffset
, but actually the initial getTop of the child view is not 0, but (mParentHeight - childHeight) / 2
, so you view if offset more than the desired offset.
找到视图design_bottom_sheet
并将其重力设置为Gravity.TOP | Gravity.CENTER_HORIZONTAL
将对其进行修复.但是,如果childHeight小于mPeekHeight,则内容视图下方将有空白区域.
Find the view design_bottom_sheet
and set its gravity to Gravity.TOP | Gravity.CENTER_HORIZONTAL
will fix it. But, if the childHeight is less than mPeekHeight, there will be blank area below you content view.
但是,如果peekHeight > childHeight
,则mMaxOffset
将小于mMinOffset
,这将导致奇怪的行为.
However, if peekHeight > childHeight
, the mMaxOffset
will less than mMinOffset
, which will cause weird behavior.
也许应该将代码更改为
mMaxOffset = Math.max((mParentHeight - mPeekHeight), mMinOffset);
由
mMaxOffset = mParentHeight - child.getHeight();
这篇关于如何使用BottomSheetDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!