Android 导航抽屉很宽

Android 导航抽屉很宽,android,navigation-drawer,Android,Navigation Drawer,我已经实现了类似的导航抽屉。幻灯片菜单显示正确,但非常宽(宽度非常长)。。当我在手机/标签中查看时,纵向和横向模式都很长。它几乎触到了屏幕的另一面。有点像全屏 当我运行AndroidHive中给出的项目时。一切都很好。我刚刚根据需求调整了XML,仅此而已 以下是抽屉列表XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/

我已经实现了类似的导航抽屉。幻灯片菜单显示正确,但非常宽(宽度非常长)。。当我在手机/标签中查看时,纵向和横向模式都很长。它几乎触到了屏幕的另一面。有点像全屏

当我运行AndroidHive中给出的项目时。一切都很好。我刚刚根据需求调整了XML,仅此而已

以下是抽屉列表XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="@drawable/list_selector" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:contentDescription="@string/desc_list_item_icon"
    android:src="@drawable/maps" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingRight="40dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/list_item_title" />

<TextView
    android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:background="@drawable/roundcircle"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="4dp"
    android:textColor="@color/counter_text_color"
    android:textStyle="bold" />

 </RelativeLayout>

这是ListView.XML,其中包含幻灯片ListView

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

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

 </android.support.v4.widget.DrawerLayout>


我不确定哪里出了问题?

列表视图的宽度需要更小。试试android:layout\u width=“240dp”

这基本上基于官方指南:


我的建议:仔细检查带有官方文档的教程:)

列表视图的宽度需要更小。试试android:layout\u width=“240dp”

这基本上基于官方指南:


我的提示:仔细检查教程和官方文档:)

您没有为
导航抽屉指定合适的宽度。如果您将宽度设置为
match\u parent
,它将(顾名思义)与父项一样大。而且您不希望导航抽屉像屏幕一样大,所以请指定大约为
240dp
的宽度。试着这样做:

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</android.support.v4.widget.DrawerLayout>

您没有为
导航抽屉指定合适的宽度。如果您将宽度设置为
match\u parent
,它将(顾名思义)与父项一样大。而且您不希望导航抽屉像屏幕一样大,所以请指定大约为
240dp
的宽度。试着这样做:

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</android.support.v4.widget.DrawerLayout>


向我们展示包含
抽屉布局的布局
我几乎100%确定其中指定的宽度太大。@XaverKapeller:更新了我的问题最好的方法是实际将NavigationView宽度设置为“包裹内容”:显示包含
抽屉布局的布局
我几乎100%确定其中指定的宽度太大。@XaverKapeller:更新了我的问题最好的方法是实际将NavigationView宽度设置为“wrap_content”:让我试一试。。我之所以要更改240dp,是因为如果屏幕分辨率更大,它最终会显示非常小的菜单。不,不是这样,dp(或dip)是与密度无关的像素。240dp在所有屏幕上的物理尺寸都是一样的。我很高兴能帮上忙!让我试试这个。。我之所以要更改240dp,是因为如果屏幕分辨率更大,它最终会显示非常小的菜单。不,不是这样,dp(或dip)是与密度无关的像素。240dp在所有屏幕上的物理尺寸都是一样的。我很高兴能帮上忙!我完成了任务。。我的错。我应该看看医生。我已经做了几个小时了。。尝试不同的方法!我完成了任务。。我的错。我应该看看医生。我已经做了几个小时了。。尝试不同的方法!