底部工作表对话框横向宽度问题

Fun*_*nts 5 android dialog width material-design bottom-sheet

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>
Run Code Online (Sandbox Code Playgroud)

代码实现:

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);
});
Run Code Online (Sandbox Code Playgroud)

出现以下问题:视频中看到的宽度不会完全显示。如果我在横向模式下从一开始就启动底页,则宽度看起来也会有所不同,因为左侧和右侧未被覆盖:

示例Lendscape

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

Md.*_*rim 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);
}
Run Code Online (Sandbox Code Playgroud)

}

=> 活动类代码

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");
});
Run Code Online (Sandbox Code Playgroud)