DrawerLayout这个类是在Support Library里的,需要加上android-support-v4.jar这个包。
<pre name="code" class="java"><?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="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" >
</FrameLayout>
<!-- 左边抽屉 这里必须放到一个布局容器里面或者组件-->
<RelativeLayout
android:layout_gravity="start"
android:layout_width="240dp"
android:background="#4876FF"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/navigation_drawer"
android:name="hk.wangdao.play.test.DrawerFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@android:color/transparent" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
注意:如果这里的第二个子元素(抽屉),这里不能直接添加为fragment,必须是布局容器或者组件(就像这里用了一个<RelativeLayout></Relativelayout>)。
DrawerLayout的第一个子元素是主要内容,即抽屉没有打开时显示的布局。这里采用了一个FrameLayout,里面什么也没放。
DrawerLayout的第二个子元素是抽屉中的内容,即抽屉布局,这里采用了一个Fragment。你可以往fragment里面添加你自己的布局。
fragment里面的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:text="呵呵"/>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent" >
</ListView>
</LinearLayout>
listview的布局:
<pre name="code" class="java"><TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:gravity="center"/>
主要的java代码:
<pre name="code" class="java">package hk.wangdao.play.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class DrawerLayoutActivity extends Activity {
private DrawerLayout mDrawerLayout;
private DrawerFragment mDrawerFragment;
private ActionBarDrawerToggle mDrawerToggle;
@SuppressWarnings("null")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerFragment = new DrawerFragment();
//影子
mDrawerLayout.setDrawerShadow(null, GravityCompat.START);
//切换
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name, R.string.hello_world){
@Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
}
@SuppressWarnings("deprecation")
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@SuppressWarnings("deprecation")
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@SuppressWarnings("deprecation")
@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;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
fragment的代码:
package hk.wangdao.play.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DrawerFragment extends Fragment {
private ListView mListView;
private String[] itemsName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawfragment, container, false);
mListView = (ListView) view.findViewById(R.id.listview);
itemsName = getResources().getStringArray(R.array.itemsname);
mListView.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.listitem,
itemsName));
return view;
}
}<span style="color:#00cccc;">
</span>
补上说明:在主要的布局中有一个<framelayout></framlayout>布局,这个布局怎么用呢?这里可以通过添加fragment来实现
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
//通过java代码添加了Fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()//获取FragmentTransaction对象
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
//给actionbar上设置title
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}