gpt4 book ai didi

android - Bottom Sheet 对话框横向宽度问题

转载 作者:行者123 更新时间:2023-12-05 01:57:36 27 4
gpt4 key购买 nike

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);
});

正在关注 issue发生的情况:视频中看到的宽度不会完全显示。如果我在横向模式下从一开始就启动 bottomsheet,宽度看起来也会不同,因为左侧和右侧没有被覆盖:

exampleLendscape

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

最佳答案

-> 你的xml布局没有问题,可以用代码解决。我在下面提供了一个解决此问题的示例。

=> Bottom Sheet 类代码

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);
}

=> Activity 类代码

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");
});

关于android - Bottom Sheet 对话框横向宽度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69093654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com