如何设置BottomSheetDialogFragment的最大宽度

时间:2016-07-18 11:57:42

标签: android android-layout bottom-sheet

问题

我使用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 /值/ 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 /值-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>

3 个答案:

答案 0 :(得分:4)

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

@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的尺寸。

答案 1 :(得分:1)

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/guidelines/components/bottom-sheets.html#bottom-sheets-specs
            getDialog().getWindow().setLayout(ViewUtils.dpToPx(450), -1);
        }
    }
}

答案 2 :(得分:0)

正如您所提到的,BottomSheetDialogFragment似乎并不尊重&#34; wrap_content&#34; (它总是在宽度上匹配父级,即使它包含的所有视图的宽度都为&#34; wrap_content&#34;)。我发现对我来说最好的解决方法是利用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);
}