当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to set the drawer using the menu function in the activity

问题描述:

I have to put a drawer layout for my app, hence I have finished the layout and functions in an activity.Now, I'm unable to use Fragments.How can I set the drawer using the menu function in the activity?Please help me.This is my main layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"`enter code here`

android:layout_height="match_parent"

android:orientation="vertical"

android:weightSum="2" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dip"

android:layout_weight="0.2"

android:background="@color/MediumSeaGreen"

android:orientation="horizontal"

android:weightSum="1">

<ImageView

android:id="@+id/notification_settings"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center"

android:layout_weight="0.85"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_action_overflow" />

<TextView

android:id="@+id/mytrip_title"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center"

android:layout_weight="0.25"

android:gravity="center"

android:text="@string/Notification"

android:textSize="@dimen/mytrip_title"

android:textStyle="bold" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dip"

android:layout_weight="1.8"

android:orientation="horizontal"

android:weightSum="1" >

<ListView

android:id="@+id/notification_list_view"

android:layout_width="match_parent"

android:layout_height="match_parent" >

</ListView>

</LinearLayout>

</LinearLayout>

And this the layout what i have designed for drawer,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

android:paddingRight="10dp"

android:layout_weight="8"

android:layout_marginLeft="10dp"

android:orientation="vertical">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal"

>

<ImageView

android:id="@+id/menu_home_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_home_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Home"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/gray" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal">

<ImageView

android:id="@+id/menu_Trips_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_trips_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Trips"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/gray" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal">

<ImageView

android:id="@+id/menu_notification_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_notification_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Notification"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/gray" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal">

<ImageView

android:id="@+id/menu_Settings_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_settings_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Settings"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/gray" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal">

<ImageView

android:id="@+id/menu_profile_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_profile_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Profile"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/gray" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="horizontal">

<ImageView

android:id="@+id/menu_friends_icon"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:padding="@dimen/menu_icon_padding"

android:src="@drawable/ic_launcher"

android:gravity="center" />

<TextView

android:layout_marginLeft="10dp"

android:id="@+id/menu_friends_text"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:gravity="center"

android:text="Friends"

android:textSize="@dimen/mytrip_title" />

</LinearLayout>

<View

android:layout_width="match_parent"

android:layout_height="1dp"`enter code here`

android:background="@color/gray" />

</LinearLayout>

while clicking the notification_settings imageview in the main layout i have to view the below designed layout for drawer function please help me

网友答案:

Try this,

  1. replace your current activity layout with this,

    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_drawer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    
    
    <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    // put your existing widgets here
    
    </FrameLayout>
    
    
    <ListView
    android:id="@+id/drawer_menu"
    android:layout_width="define your width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:layout_weight="1"
    />
    

  2. Instantiate drawer layout, your menu list, action bar and toggle in your activity.

  3. Define suitable row layout for the list and set the data. You can easily find sample code from here.

分享给朋友:
您可能感兴趣的文章:
随机阅读: