赶着时间回去跑步,废话不多说直接上图
源码地址:
http://download.csdn.net/detail/u013922681/8950431
DrawerActivity.java
package com.example.drawerlaout2;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class DrawerActivity extends Activity
{
private ListView lv;
private String[] strList;
private DrawerLayout mDrawerLayout = null;
private TitleLayout layout = null;
private FragmentManager fragmentManager = getFragmentManager();
private LinearLayout leftRoot = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initComp();
initData();
initFragment();
}
private void initFragment() {
// TODO Auto-generated method stub
//默认打开DemoFragment1
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new DemoFrag1()).commit();
}
private void initData() {
// TODO Auto-generated method stub
layout.getBackView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
strList = getResources().getStringArray(R.array.items);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strList));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
lv.setItemChecked(position, true);
mDrawerLayout.closeDrawer(leftRoot); //关掉抽屉
Fragment fragment = null;
System.out.println("pos = "+position+":position % 3"+position % 3);
switch (position % 3) {
case 0:
fragment = new DemoFrag1();
break;
case 1:
fragment = new DemoFrag2();
break;
case 2:
fragment = new DemoFrag3();
break;
default:
break;
}
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
});
}
private void initComp() {
// TODO Auto-generated method stub
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
layout = (TitleLayout) findViewById(R.id.title);
lv = (ListView) findViewById(R.id.left_drawer);
leftRoot = (LinearLayout) findViewById(R.id.left_root);
}
}
activity_main.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"
tools:context=".DrawerActivity" >
<!--
<include
android:id="@+id/title"
layout="@layout/activity_title"/>
-->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.drawerlaout2.TitleLayout
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
<!-- The navigation drawer -->
<!-- <LinearLayout
android:layout_width="200dp"
android:layout_gravity="left"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/left_drawer"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout> -->
<include
layout="@layout/info"
android:layout_width="200dp"
android:layout_gravity="left"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
**
DemoFrag1~DemoFrag3在我之前的博客中有源码,不再贴上
**
TitleLayout.java自定义组件
package com.example.drawerlaout2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class TitleLayout extends LinearLayout {
private ImageView back;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater.from(context).inflate(R.layout.activity_title, this);
back = (ImageView) findViewById(R.id.iv);
}
public ImageView getBackView(){
if(back!=null){
return back;
}
return null;
}
}
activity_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#4DFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:background="#4DFFFF"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv"
android:layout_width="40dp"
android:layout_height="match_parent"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/ied" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="U b e r 优 步"
android:textColor="#FFD2D2"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#898989" />
</LinearLayout>
转载表明出处