10

问题

我正在BottomSheetDialogFragment为我的模态底部工作表使用 ,并希望设置最大宽度,以便在平板电脑/大屏幕BottomSheet上不占据屏幕的整个宽度。我该如何解决这个问题?谢谢!

相关代码和资源

fragment_bottomsheet.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"
    style="@style/BottomSheetStyle">

    <GridLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"
        android:columnCount="3"
        android:paddingTop="16dp"
        android:paddingBottom="8dp"
        android:paddingRight="8dp"
        android:paddingLeft="8dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/image1"
            android:text="Open"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/image2"
            android:text="Save"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/image3"
            android:text="Send"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/image4"
            android:text="Upload"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/image5"
            android:text="Share"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/iamge6"
            android:text="More"/>

    </GridLayout>

</android.support.design.widget.CoordinatorLayout>

res/values/styles.xml

<style name="BottomSheetStyle">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>

res/values-w600dp/styles.xml

<style name="BottomSheetStyle">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">640dp</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>
4

4 回答 4

8

我找到了一个适合我的解决方案:

@Override
public void onResume() {
    super.onResume();

    // Resize bottom sheet dialog so it doesn't span the entire width past a particular measurement
    WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels < 1280 ? metrics.widthPixels : 1280;
    int height = -1; // MATCH_PARENT
    getDialog().getWindow().setLayout(width, height);
}

本质上,这允许我BottomSheet根据显示动态指定我的尺寸。

于 2016-07-20T09:29:51.010 回答
5

考虑到设备的像素密度和方向的 OP 答案的变体:

public final class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
    ...

    private static int dpToPx(int dp) {
        // https://developer.android.com/guide/practices/screens_support.html#dips-pels
        float density = Resources.getSystem().getDisplayMetrics().density;
        return (int) ((dp * density) + 0.5f);
    }

    @Override
    public void onResume() {
        super.onResume();

        Configuration configuration = getActivity().getResources().getConfiguration();
        if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&
                configuration.screenWidthDp > 450) {
            // you can go more fancy and vary the bottom sheet width depending on the screen width
            // see recommendations on https://material.io/components/sheets-bottom#specs
            getDialog().getWindow().setLayout(ViewUtils.dpToPx(450), -1);
        }
    }
}
于 2017-09-15T11:51:39.390 回答
0

正如您所提到的,BottomSheetDialogFragment 似乎不尊重“wrap_content”(它总是在宽度上匹配父级,即使它包含的所有视图都具有“wrap_content”的宽度)。我发现对我来说最好的解决方法是利用 OnGlobalLayoutListener 来监听适当的时间来调整宽度。例子:

 @Override
public void setupDialog(final Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View v = View.inflate(getActivity(), R.layout.my_bottom_sheet, null);
    View viewWithGreatestWidth = v.findViewById(R.id.my_super_wide_view);
    // Your view setup code goes here ....

    if(getResources().getBoolean(R.bool.isTablet)) {
        v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                // Can also just use your own preset "max width" value here;
                // This code just lets you fake "wrap_content" value
                getDialog().getWindow().setLayout(viewWithGreatestWidth.getMeasuredWidth(), WindowManager.LayoutParams.WRAP_CONTENT);
            }
        });
    }

    dialog.setContentView(v);
}
于 2016-10-20T23:14:40.453 回答
0

如果你想纯粹在xml中做,你可以设置一个样式并使用android:maxWidth

例如:

<LinearLayout
   android:id="@+id/bottom_sheet"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:background="@color/white"
   app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
   style="@style/styleWithFixedWidth"
 >

并且在styles.xml

<style name="styleWithFixedWidth">
  <item name="android:maxWidth">550dp</item>
</style>
于 2021-10-12T14:00:52.847 回答