赞
踩
DrawerLayout是V4 Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。
本文程序效果:
1
2
3
<?xml version="1.0" encoding="utf-8"?>
<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 ···没滑动前显示的页面布局 -->
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:background="#f00"
>
<TextView
android:id="@+id/tv_menu"
android:layout_width="40dp"
android:layout_height="match_parent"
android:text="菜单"
android:gravity="center"
android:clickable="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="显示的页面信息"
android:gravity="center"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="其他内容"
android:gravity="center"
/>
</LinearLayout>
<!-- The navigation drawer ···滑动出来后显示的菜单栏页面布局 -->
<LinearLayout
android:id="@+id/left"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
android:layout_gravity="start"
android:layout_gravity="end"
mDrawerLayout.closeDrawers();
mDrawerLayout.openDrawer(left);
//上面的left既可以是菜单栏的布局对象,也可以是菜单栏的布局id
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View view, float v) {
}
@Override
public void onDrawerOpened(View view) {
Toast.makeText(MyActivity.this, "打开了侧边栏" , Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerClosed(View view) {
Toast.makeText(MyActivity.this, "关闭了侧边栏" , Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
其实到这里DrawerLayout已经够用了,但是很多文章介绍什么ActionBar,搞一大堆解释,而且占了文章的大部分内容!其实没必要,现在ActionBar用得很少了。
package com.example.NavigationDemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
/**
* Called when the activity is first created.
*/
public class MyActivity extends Activity {
private List<String> mPlanetTitles = new ArrayList<String>();
private DrawerLayout mDrawerLayout;
private LinearLayout left;
private ListView mDrawerList;
private TextView tv_menu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
initData();
initEvent();
}
private void initView() {
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
left= (LinearLayout) findViewById(R.id.left);
tv_menu= (TextView) findViewById(R.id.tv_menu);
}
private void initData() {
for (int i = 0; i < 10; i++) {
mPlanetTitles.add("left---Line" + i);
}
//创建并设置适配器
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.activity_list_item, android.R.id.text1, mPlanetTitles);
mDrawerList.setAdapter(adapter);
}
private void initEvent() {
//设置ListView的点击条目事件 ,选中后让菜单栏消失
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MyActivity.this, "点击了" + position, Toast.LENGTH_LONG).show();
mDrawerLayout.closeDrawers(); //关闭侧边栏的菜单
}
});
//点击菜单按钮,显示菜单
tv_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawerLayout.openDrawer(left); //显示左边的菜单栏的控制
}
});
//监听侧边菜单栏打开或关闭的状态
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View view, float v) {
}
//打开菜单栏后触发的方法
@Override
public void onDrawerOpened(View view) {
Toast.makeText(MyActivity.this, "打开了侧边栏" , Toast.LENGTH_LONG).show();
}
//关闭菜单栏后触发的方法
@Override
public void onDrawerClosed(View view) {
Toast.makeText(MyActivity.this, "关闭了侧边栏" , Toast.LENGTH_LONG).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
}
}
源码资源也提供給大家参考:
http://download.csdn.net/detail/wenzhi20102321/9823238
很多时候像这种兼容包的工程运行不了,报的SDK的错,可能要将SDK的最低版本调低,我这里调了 才能运行!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。