底部弹出式对话框横屏宽度问题

5

bottom_sheet_image_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="62dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/llAddDriverPic"
    android:background="?android:attr/selectableItemBackground"
    android:padding="8dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/baseline_add_photo_alternate_24"
        android:layout_margin="8dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_picture"
        android:layout_gravity="center_vertical"
        android:padding="8dp"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/llRemoveDriverPic"
    android:background="?android:attr/selectableItemBackground"
    android:padding="8dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/baseline_no_photography_24"
        android:layout_margin="8dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/remove_picture"
        android:layout_gravity="center_vertical"
        android:padding="8dp"/>

</LinearLayout>

代码实现:
BottomSheetDialog bsDialog = new BottomSheetDialog(getContext());
bsDialog.setContentView(R.layout.bottom_sheet_image_dialog);

bsDialog.setOnShowListener(dialog -> {
    BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior();
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
});

以下是发生的问题:在视频中看到的宽度不会完全显示。 如果我在横向模式下从开头开始启动底部表单,则左侧和右侧未被覆盖的宽度也会有所不同:

exampleLendscape

可能的问题是什么? 在显示对话框之前是否需要预定义宽度?

2个回答

10

-> 你的xml布局没有问题,这个问题可以在代码中解决。以下是一个解决此问题的示例。

=> 底部弹出窗口类代码

public class BottomSheet extends BottomSheetDialogFragment {

private View view;
private BottomSheetBehavior mBottomBehavior;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);

  //Do your logic code here

    return view;
}

@Override
public void onStart() {
    super.onStart();
    mBottomBehavior = BottomSheetBehavior.from((View) view.getParent());
    mBottomBehavior.setMaxWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    mBottomBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

=> 活动类代码

Button button = findViewById(R.id.button);

button.setOnClickListener(v -> {

//To open the bottom sheet, NB. make sure 'BottomSheet' is the name you declared Bottom sheet class

    BottomSheet bottomSheet = new BottomSheet();
    bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
});

0
在 Kotlin 中:
val dialog = BottomSheetDialog(context).apply {
    setContentView(bottomSheet.root)
    behavior.maxWidth = ViewGroup.LayoutParams.MATCH_PARENT
    behavior.state = BottomSheetBehavior.STATE_EXPANDED
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接