Android DrawerLayout 抽屉

DrawerLayout 在supportV4 Lib在。类似的开源slidemenu如,DrawerLayout父类ViewGroup,自定义组件基本上都是扩展类。

android.support.v4.widget.DrawerLayout

以下是个简单的使用方法演示。点左上角的button 打开抽屉菜单。

点击相应的ITEM 切换相应的内容,内容显示使用Fragment。这里没用到ActionBar来做切换

<?

xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" > <!-- 这里是内容区域 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#9370DB"
android:orientation="horizontal" > <ImageView
android:id="@+id/slide"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="fitCenter"
android:layout_gravity="center_vertical"
android:src = "@drawable/slide"
/> </LinearLayout> <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout> <!-- 抽屉内容 -->
<ListView
android:id="@+id/drawer_content"
android:layout_width="280dp"
android:layout_height="match_parent"
android:listSelector="#336699"
android:divider="#B0C4DE"
android:dividerHeight="1px"
android:layout_gravity="start"
android:background="#8470FF" /> </android.support.v4.widget.DrawerLayout>
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class DrawerLayoutActivity extends FragmentActivity { private DrawerLayout mDrawerLayout;
private ListView drawerContent;//抽屉内容 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerContent = (ListView) findViewById(R.id.drawer_content);
mDrawerLayout.setDrawerListener(new DemoDrawerListener());
addHeader(drawerContent);//ListView Header头
drawerContent.setAdapter(new MyAdapter());
drawerContent.setOnItemClickListener(new DrawerItemClickListener());
findViewById(R.id.slide).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mDrawerLayout.openDrawer(drawerContent);//打开抽屉内容
}
}); //显示默认的HOME
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit(); } private void addHeader(ListView mDrawer2) {
View view = LayoutInflater.from(this).inflate(R.layout.list_header,null);
mDrawer2.addHeaderView(view);
} static String titles[] = { "主页", "公司", "附近", "设置" }; class MyAdapter extends BaseAdapter { @Override
public int getCount() {
return titles.length;
} @Override
public Object getItem(int p) {
return titles[p];
} @Override
public long getItemId(int arg0) {
return 0;
} @Override
public View getView(int p, View contentView, ViewGroup arg2) {
String title = titles[p];
View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_list, null);
TextView textView = (TextView) v.findViewById(R.id.text);
textView.setText(title);
return v;
} } @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<? > parent, View view, int position,long id) {
showFragmentById(position);
mDrawerLayout.closeDrawer(drawerContent);
}
} //切换到相应的Fragment
public void showFragmentById(int position) {
Fragment f = null;
switch (position) {
case 1:
f = new HomeFragment();
break;
case 2:
f = new CompanyFragment();
break;
case 3:
f = new NearFragment();
break;
case 4:
f = new SettingFragment();
break;
default:
break;
}
if (f == null) {
return;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
} //回调函数,打开或关闭时能够做一些事情
private class DemoDrawerListener implements DrawerLayout.DrawerListener {
@Override
public void onDrawerOpened(View drawerView) { //打开时调用
} @Override
public void onDrawerClosed(View drawerView) { //关闭时调用
} @Override
public void onDrawerSlide(View drawerView, float slideOffset) {//滑动过程中
} @Override
public void onDrawerStateChanged(int newState) {
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
} }

这里仅仅写HomeFragment代码,其他几个内容都是一样的。

public class HomeFragment extends Fragment {

    public HomeFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fg, container,false);
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("Home");
} }

home_fg.xml

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

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#9370DB"
android:textStyle="bold"
android:layout_centerInParent="true"
/> </RelativeLayout>

Android DrawerLayout 抽屉的更多相关文章

  1. Android笔记:DrawerLayout抽屉布局的使用

    DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn.. 如下示,只要从屏幕侧边滑一下,或者点击左上角的图标,抽屉就会出来. DrawerLayout要点: 1.使用Drawer ...

  2. 浅谈DrawerLayout(抽屉效果)

    DrawerLayout是V4包下提供的一种左滑右滑抽屉布局效果. 实现效果如下: 因为是官方提供的,所以使用起来也相对的比较简单. DrawerLayout 提供 1.当界面弹出的时候,主要内容区会 ...

  3. Android - DrawerLayout

    Android DrawerLayout 的使用 Android L Android Studio 1.4 从主视图左侧能抽出一个导航栏,效果图:  点击后弹出新界面:  新界面也可以抽出左侧导航栏 ...

  4. android DrawerLayout 侧边栏实现

    现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现. 主要实现方法是:用DrawerLayout 作为界面根控件.在DrawerLayout里面第一个View为当前界面主内容: ...

  5. android中抽屉布局DrawerLayout的使用

    这个抽屉布局类似于手机QQ的主界面,点击左上角头像,会从界面左侧弹出一个抽屉,展示一些内容. 首先是布局界面activity_main.xml: <?xml version="1.0& ...

  6. Android DrawerLayout Plus 增强版抽屉菜单

    DrawerLayout是官方提供的侧滑菜单,相比SliddingMenu,它更加轻量级.默认情况下,DrawerLayout可以设置左侧或者右侧滑出菜单.如下,   xml布局: <!-- & ...

  7. Android导航抽屉-Navigation Drawer

    Google今年七月份的时候更新了他们的Google+应用,采用了新的导航方式并抛弃了navigationdrawer.一时之间,又引发了一系列关于NavigationDrawer利弊的讨论,不过对于 ...

  8. DrawerLayout(抽屉效果)

    DrawerLayout是V4包下提供的一种左滑右滑抽屉布局效果. 实现效果如下: 因为是官方提供的,所以使用起来也相对的比较简单. DrawerLayout 提供 1.当界面弹出的时候,主要内容区会 ...

  9. Android DrawerLayout 高仿QQ5.2双向侧滑菜单

    1.概述 之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣:另一方面 ...

随机推荐

  1. Core Data 和 sqlite3的性能对比【图】3gs,iPhone4,4s,5的性能测试。

    demo 和源码再此下载  :http://download.csdn.net/detail/hherima/5603797

  2. HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个n个数的序列,当中组成的数仅仅有0-n,我们能够进行这么一种操作:把第一个数移到最 ...

  3. Missile:双状态DP

    题目 描写叙述 Long , long ago ,country A invented a missile system to destroy the missiles from their enem ...

  4. VSTO 向office文档中插入内容

    原文:VSTO 向office文档中插入内容 Word: Word.Selection sec = ThisAddIn.appWord.Selection;            sec.Insert ...

  5. MySQL多表查询之外键、表连接、子查询、索引

    MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...

  6. ExtJS学习-------Ext正确Dom操作:Ext.get Ext.fly Ext.getDom

    详细实例: (1)创建JSP文件.引入CSS和js文件,加入三个Div <%@ page language="java" import="java.util.*&q ...

  7. SQL Server Database 维护计划创建一个完整的备份策略

     SQL Server维护计划Maintenance Plan这是一个非常有用的维护工具,能够完成大部分的数据库维护任务,通过这些功能包.您可以省略大量的编码时间. 介绍的不是非常多,特此补上一篇 ...

  8. UVa-Palindromes

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. 盒子游戏(The Seventh Hunan Collegiate Programming Contest)

    盒子游戏 有两个相同的盒子,其中一个装了n个球,另一个装了一个球.Alice和Bob发明了一个游戏,规则如下:Alice和Bob轮流操作,Alice先操作.每次操作时,游戏者先看看哪个盒子里的球的数目 ...

  10. hdu 1561 The more, The Better (依赖背包 树形dp)

    题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...