码迷,mamicode.com
首页 > 移动开发 > 详细

安卓笔记:DrawerLayout抽屉布局的使用

时间:2016-12-28 20:09:15      阅读:703      评论:0      收藏:0      [点我收藏+]

标签:actionbar   extends   listener   dem   ima   str   undle   nbsp   match   

DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn。。

要点:

1.使用DrawerLayout时,在xml布局中,把主界面的布局放在前面,后面才放上抽屉里的布局内容

2.记得为抽屉内的布局加上android:layout_gravity="left"或"start",指明抽屉出现在哪一侧。

 

代码如下所示:

activity_drawer.xml

<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.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/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- 抽屉的内容 -->

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#ffffff"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp" />
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">DrawerLayoutTest</string>
    <string name="action_settings">Settings</string>
    <string name="drawer_close">关闭抽屉</string>
    <string name="drawer_open">打开抽屉</string>
    
    <string-array name="planets_array">
        <item>主页</item>
        <item>发现</item>
        <item>收藏</item>
        <item>设置</item> 
        <item>草稿</item>
        <item>历史</item>
        <item>模式</item>
    </string-array>
    
</resources>

DrawerLayoutDemo.java

package com.example.drawerlayouttest;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;

public class DrawerLayoutDemo extends Activity
{

    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ListView mDrawerList;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        // 初始化ListView
        initListView();
        //添加抽屉开关
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close)
        {
            //抽屉关闭
            public void onDrawerClosed(View view)
            {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

           //抽屉打开
            public void onDrawerOpened(View drawerView)
            {
                invalidateOptionsMenu(); 
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);       
        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        // getActionBar().setHomeButtonEnabled(true);
    }

    private void initListView()
    {
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mPlanetTitles));
        
        // 设置监听事件
        mDrawerList.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                mDrawerList.setItemChecked(position, true);
                setTitle(mPlanetTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);
            }
        });
    }
   
    //Activity运行后的回调
    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    //配置改变时,不用重新启动Activity
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    //设置选项菜单
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

抽屉开关的图标:

技术分享

代码运行后,打开抽屉,效果如图:

技术分享

 

安卓笔记:DrawerLayout抽屉布局的使用

标签:actionbar   extends   listener   dem   ima   str   undle   nbsp   match   

原文地址:http://www.cnblogs.com/expiator/p/6230188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!