如何设置导航抽屉的右边距?

5
根据标题,如何像Google的材料设计(参见下文)一样将导航抽屉的右边距设置为56dp?我已经在DrawerLayout的第二个视图中尝试了android:layout_marginRight="56dp",但它看起来非常奇怪(超过56dp,并且与下面的Google材料设计图片非常不同)。

enter image description here

2个回答

0

请尝试关注导航抽屉的宽度,它应该在240dp320dp之间。它将自动调整正确的边距。


这是一个包含导航抽屉片段的 XML 文件示例。在这里,片段的宽度设置在 dimens 文件夹中,为 280dp。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />

            <it.neokree.materialtabs.MaterialTabHost
                android:id="@+id/materialTabHost"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_below="@+id/app_bar"
                app:accentColor="@color/colorAccent"
                app:hasIcons="true"
                app:iconColor="@android:color/white"
                app:primaryColor="@color/colorPrimary" />
        </LinearLayout>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/materialTabHost"
            android:layout_weight="1" />
    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.example.fragments.FragmentDrawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

现在这是一个示例,它是导航抽屉的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context="com.example.fragments.FragmentDrawer">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

如果您在dimens文件夹中设置宽度而不是硬编码,那将是一个很好的实践。


0

我建议您关注 DeawerLayout 的第二个 View 的宽度,应该在 240dp 到 320dp 之间。

想一想,您成功地在右侧给了 navigation drawer 56 dp 的右边距,现在您想在 landscape 模式下检查它,更多的惊喜正在等待着您。


哈哈,我认为你是对的。如果在横向模式下有一个56dp的右边距,它看起来会非常奇怪。那么,在谷歌的材料设计指南中指定56dp的意义是什么呢?这是否意味着我不应该遵循这些指南? - karansky
@Sevin,他们可能是在说没有人是完美的,或者在横向和纵向模式下有两个不同的边距,我认为这可能不太合适。 - Pankaj Nimgade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接