Android底部工作表模式(对话框)不';我不能完全打开
我正在尝试在我的应用程序中单击一个按钮来显示底部工作表对话框。但对话正在部分打开。我想完全打开按钮点击对话框 我尝试了以下代码 MainActivity.javaAndroid底部工作表模式(对话框)不';我不能完全打开,android,android-layout,material-design,Android,Android Layout,Material Design,我正在尝试在我的应用程序中单击一个按钮来显示底部工作表对话框。但对话正在部分打开。我想完全打开按钮点击对话框 我尝试了以下代码 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCon
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());
}
});
}
}
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);
}
}
}
CustomBottomSheetDialogFragment.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());
}
});
}
}
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);
}
}
}
活动\u 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>
当前行为:
Android支持库Vesion 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任何新版本)
我已经在新版本中测试了你的代码。它工作正常
希望对您有所帮助。不要使用片段使用BottomSheetDialog。
示例演示已上载到
对于刚刚设置的模型对话框
bottomshetDialod.setCanceledOnTouchOutside(false);
对我有用。你应该为你的行为设置peek height
同样的事情也发生在我身上。它背后的奇怪原因是工具栏
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
}
如果移除工具栏,则底部工作表将全屏显示
我不知道背后的原因。但在移除工具栏后,它工作正常。
您可以试试。在这一行中,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();
}
});
我遵循了这些步骤,这有助于我创建BottomSheetDialog
步骤1:要创建bottomsheetbehavior
,需要默认存在的对话框的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动态计算屏幕高度
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
这个代码对我有用
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
}
@好的。请接受我的回答以帮助其他人。:)我们已经在使用最新版本28.0.0。然而,这个问题正在发生!可能会对答案进行一些细化,例如:bottomSheetBehavior.setpeek高度(size)代码>我不知道为什么会这样,但我认为最好将其指向STATE\u COLLAPSED。只需将其设置为愿望值:app:behavior\u peek height=“180dp”这对我有效,然而,我不得不使用R.id.design\u bottom\u sheet
而不是android.support.design.R.id.design\u bottom\u sheet
。我猜这是屏幕高度的一个因素,所以当你移除工具栏时,会有更多可用空间。在我的例子中,它只发生在屏幕方向是水平的时候,我忘了说我目前在kotlin开发。