问题
I was trying to implement material design for one of my project and here, the following is mentioned,
The maximum width of the nav drawer is 5 times the standard increment (56dp on mobile and 64dp on tablet).
So I would like to know how to implement these width for navigation drawer for mobile and tablets. I want to make different width to respective devices, either phone or tablets.
I would love to use XML even if this also can be done using Java.
Thank you.
回答1:
Create values-sw600dp folder in your project and add dimens.xml file in it. Define width in dimens.xml file of both values and values-sw600dp folder
For values-sw600dp/dimens.xml
    <resources>
    <dimen name="width">300dp</dimen></resources>
For values/dimens.xml
    <resources><dimen name="width">150dp</dimen>   </resources>
Call this in this manner
   android:layout_width="@dimens/width"
    回答2:
You need to define two different dimens.xml corresponding to each device, then you just need to reference the dimen from your layouts
├── res
│   ├── values
│   │   ├── dimens.xml // Contains an item with 56dp
│   ├── values-sw600dp
│   │   ├── dimens.xml // Contains an item with 64dp
    回答3:
In java, I did the following to fix the width of portrait and landscape modes, you can extend it to the display using ifs with display metrics.
    // Setup Navigation drawer
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    View drawer = findViewById(R.id.scrimInsetsFrameLayout);
    // Fix right margin to 56dp (portrait)
    ViewGroup.LayoutParams layoutParams = drawer.getLayoutParams();
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        layoutParams.width = displayMetrics.widthPixels - (56 * Math.round(displayMetrics.density));
    }
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        layoutParams.width = displayMetrics.widthPixels + (20 * Math.round(displayMetrics.density)) - displayMetrics.widthPixels / 2;
    }
BTW in your case I should use the answers before (dimen for each version/mode/device).
来源:https://stackoverflow.com/questions/28574506/different-width-for-navigation-drawer-in-tablet-and-phone