导航抽屉不适合活动

时间:2015-06-25 17:28:35

标签: android

我是Android新手。我想使用NavigationDrawer,但它不适合Activity。我在ActionBarTab中使用FragmentActivity

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.basket.MainActivity" >
  <FrameLayout android:id="@+id/fragment_content"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1" />

  <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ListView         
        android:padding="0dp" 
        android:id="@+id/left_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

enter image description here

2 个答案:

答案 0 :(得分:1)

Because you are new with this I recommend you follow these instructions to create a new Navigation Drawer If you follow the recommendations you will see that here it is doing inversely that the way you're doing <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"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout> Design 1. android.support.v4.widget.DrawerLayout 1.1 FrameLayout (Main content view) 1.2 ListView (Navigation drawer) With the image below you can understand better how it works But if you want to continue doing like you show in your question you have to delete paddings attribute in your DrawerLayout like the example below <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" tools:context="com.example.basket.MainActivity" > <FrameLayout android:id="@+id/fragment_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:padding="0dp" android:id="@+id/left_drawer" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="#FFFFFF" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>

答案 1 :(得分:0)

It is because of these lines in your xml: android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" They are adding an inner padding which prevents your drawer from reaching the edges of the fragment. Removing these lines will likely cause your other views to extend to the edges as well, so you'll have to adjust your layout as necessary.