我在我的活动中使用BottomSheetDialogFragment,对话框在纵向模式下显示全高,但在切换到横向模式时不显示.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomBottomSheetDialog customBottomSheetDialog = new CustomBottomSheetDialog();
customBottomSheetDialog.show(getSupportFragmentManager(),customBottomSheetDialog.getTag());
}
}
Run Code Online (Sandbox Code Playgroud)
CustomBottomSheetDialog
public class CustomBottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return View.inflate(getContext(), R.layout.view_config, null);
}
}
Run Code Online (Sandbox Code Playgroud)
CustomBottomSheetDialog布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#fdf107"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="196dp"
android:gravity="center"
android:textColor="@color/colorAccent"
android:text="BottomSheetDialogFragment"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在横向模式下,我必须拖动BottomSheetDialogFragment才能看到整个内容.
ave*_*raj 24
这个问题的解决方案是.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
FrameLayout bottomSheet = (FrameLayout)
dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
behavior.setPeekHeight(0); // Remove this line to hide a dark background if you manually hide the dialog.
}
});
}
Run Code Online (Sandbox Code Playgroud)
Mak*_*osH 13
这对我有用,是最干净的方法:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}
Run Code Online (Sandbox Code Playgroud)
该ViewTreeObserver解决方案对我不起作用,但我在这里找到了一个更好的解决方案并将其转换为 Kotlin。这个没有昂贵的计算浪费,ViewTreeObserver并且很好地将功能捆绑到类中。
class ExpandedBottomSheetDialog(context: Context) : BottomSheetDialog(context) {
override fun show() {
super.show()
// androidx should use: com.google.android.material.R.id.design_bottom_sheet
val view = findViewById<View>(R.id.design_bottom_sheet)
view!!.post {
val behavior = BottomSheetBehavior.from(view)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
}
}
}
Run Code Online (Sandbox Code Playgroud)
多亏@avez raj和我在中撰写的《与外界接触防止BottomSheetDialogFragment被解雇》onCreateDialog()。
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener {
// For AndroidX use: com.google.android.material.R.id.design_bottom_sheet
val bottomSheet = dialog.findViewById<View>(
android.support.design.R.id.design_bottom_sheet) as? FrameLayout
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
return dialog
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以简单地执行以下操作:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog?.let {
val sheet = it as BottomSheetDialog
sheet.behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
// rest of your stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6951 次 |
| 最近记录: |