[英]Android Bottom Sheet Modal (Dialog) doesn't open completely
我试图在单击按钮时在我的应用程序中显示底部工作表对话框。 但是对话框正在部分打开。 我想在单击按钮时完全打开对话框。
我试过以下代码。
主活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View showModalBottomSheet = findViewById(R.id.as_modal);
showModalBottomSheet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Initializing a bottom sheet
BottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
//show it
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
});
}
}
CustomBottomSheetDialogFragment.java
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.dialog_modal, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
}
活动_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.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
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>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<Button
android:id="@+id/as_modal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:text="@string/modal" />
</android.support.v4.widget.NestedScrollView>
您可以在此处找到该项目的链接:
当前行为:
在 setUpDialog 方法中的这一行可以解决问题
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
该方法在 onCreate 方法中写入您的 CustomBottomSheetDialogFragment
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(R.id.design_bottom_sheet);
CoordinatorLayout lyout = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setPeekHeight(bottomSheet.getHeight());
lyout.getParent().requestLayout();
}
});
Android 支持库版本 23.2.0 中的BottomSheetDialogFragment
和BottomSheetDialog
存在一些问题。
您可以在 Android 支持库,修订版 23.2.1(2016 年 3 月)部分中查看此文档。
因此,解决方案是将您的com.android.support:design
版本更新到 23.2.0 以上。 (23.2.1、23.3.0、23.4.0无论是新版本)。
我已经在新版本中测试了您的代码。 它正常工作。
希望有帮助。
你应该为你的行为设置peekHeight
我遵循了这些步骤,这对我的BottomSheetDialog dialog
帮助。
Step1:要创建一个BottomSheetBehaviour
,您需要默认存在的对话框的FrameLayout
。
FrameLayout bottomSheet = dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
如果您使用的是 androidx,请使用
FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
步骤 2:将底部工作表行为设置为展开状态并设置查看高度
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
behavior.setPeekHeight(screenHeight / 2);
behavior.setHideable(false);
步骤 3:不允许对话框在外部触摸时取消
dialog.setCanceledOnTouchOutside(false);
注意:您可以使用 DisplayMetrics 动态计算 screenHeght。
DisplayMetrics screenMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(screenMetrics);
int screenHeight = screenMetrics.heightPixels;
int screenWidth = screenMetrics.widthPixels;
将这样的内容添加到 onCreateView 方法中:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
View bottomSheetInternal = d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
return inflater.inflate(R.layout.your_bottomsheet_content_layout, container, false);
}
供 AndroidX 使用
com.google.android.material.R.id.design_bottom_sheet
而不是
android.support.design.R.id.design_bottom_sheet
不要使用片段使用BottomSheetDialog。 示例演示上传到https://github.com/bita147/BottomSheetDialog
对于模型对话框刚刚设置
bottomshetDialod.setCanceledOnTouchOutside(false);
为我工作。
同样的事情也发生在我身上。 其背后的奇怪原因是Toolbar
如果您删除工具栏,则底部表格将全屏显示。
我不知道背后的原因。 但是删除工具栏后它工作正常。 你可以试试。
这段代码对我有用。
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dialog!!.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheetInternal: FrameLayout? =
d.findViewById(com.google.android.material.R.id.design_bottom_sheet)
BottomSheetBehavior.from(bottomSheetInternal!!).state =
BottomSheetBehavior.STATE_EXPANDED
}
val view = inflater.inflate(R.layout.bottom_sheet_modal, container, false)
return view
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.