学习使用DrawerLayout

前几天在极客学院上看到的视频 自己手敲了一遍,还没有注册的可以 点击这个链接:注册链接领取会员

正文:
activity_main.xml:

<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" >
    </FrameLayout>

    <!-- the navigation view 子视图-->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffcc"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" >
    </ListView>

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

使用Drawerlayout 注意事项:
1、主内容视图必须是Drawerlayout的第一个子视图。
2、主内容视图的宽和高匹配父视图,即”match_parent”
3、必须显示指定抽屉视图(如ListView )的Android:layout_gravity属性
1)android:layout_gravity=”start” 菜单从左向右滑出
2)android:layout_gravity=”end” 菜单从右向左滑出
3)使用 left 和 right 也能达到同样的效果但是google 不推荐
4、抽屉视图的宽度以 dp 为单位,请不要超过 320dp (目的:为了一直能看到内容视图)
5、最外层必须是DrawerLayout 这是v4包的。android.support.v4.widget.DrawerLayout

Activity:

package com.example.zyh;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
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;

public class MainActivity extends Activity implements OnItemClickListener {

    private DrawerLayout mDrawerLayout;
    private ListView mListView;
    private ArrayList<String> mDatas;
    private ArrayAdapter<String> mAdapter;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private String mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = (String) getTitle();
        initView();
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_launcher, R.string.drawer_open,
                R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
                getActionBar().setTitle(mTitle);
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {

                getActionBar().setTitle("请选择");
                super.onDrawerOpened(drawerView);
            }
        };
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

    }

    private void initView() {

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mListView = (ListView) findViewById(R.id.left_drawer);
        mDatas = new ArrayList<String>();
        mDatas.add("你赢了");
        mDatas.add("陪你武极天下!");
        mDatas.add("你输了");
        mDatas.add("陪你东山再起!");
        mAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, mDatas);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // 点击菜单中的选项卡 进入不同页面,这里使用 android 3.0 后出现的 Fragment
        String content = mDatas.get(position);
        ContentFragment contentfg = new ContentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("content", content);
        contentfg.setArguments(bundle);
        FragmentManager manager = getFragmentManager();
        manager.beginTransaction().replace(R.id.content_frame, contentfg)
                .commit();

        mDrawerLayout.closeDrawer(mListView);//关闭 菜单
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
         mActionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

目的 :学习记录,方便后续查询。

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值