http://blog.csdn.net/abc_123_linbin/article/details/18261201
strings.xml 檔案代碼
[java]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">android_fragment_menu</string>
<string name="action_settings">settings</string>
<string name="hello_world">hello world!</string>
<string name="sys">系統管理</string>
<string name="use">使用者管理</string>
<string name="product">産品管理</string>
</resources>
menu檔案夾下 main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/sys"
android:orderincategory="100"
android:showasaction="always"
android:title="@string/sys"/>
android:id="@+id/use"
android:title="@string/use"/>
android:id="@+id/product"
android:title="@string/product"/>
</menu>
sys.xml (加載到 fragment)
use.xml (加載到 fragment)
<linearlayout 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/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者管理界面"
android:textsize="30sp"
/>
</linearlayout>
produce.xml (加載到listfragment)
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingleft="8dp"
android:paddingright="8dp">
<!-- 注意 id 的使用 -->
<listview android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00ff00"
android:layout_weight="1"
android:drawselectorontop="false"/>
</linearlayout>
activity_main.xml 檔案代碼
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".mainactivity"
android:id="@+id/main"
>
</relativelayout>
sysfragment extends fragment
sysfragment.java 代碼
package com.example.android_fragment_menu;
import android.app.fragment;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
public class sysfragment extends fragment {
@override
public void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
}
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
view view =inflater.inflate(r.layout.sys, null);
return view;
public void onpause() {
super.onpause();
}
usefragment extends fragment
usefragment.java 代碼
public class usefragment extends fragment {
view view=inflater.inflate(r.layout.use, null);
producfragment extends listfragment
producfragment.java代碼
import java.util.arraylist;
import java.util.list;
import android.app.listfragment;
import android.widget.arrayadapter;
import android.widget.listview;
public class producfragment extends listfragment {
private arrayadapter<string> adapter=null;
private list<string> list;
/**
* 資料的初始化
* @return
*/
public list<string> getdata()
{
list<string> list=new arraylist<string>();
for(int i=0;i<30;i++)
{
list.add("item"+i);
}
return list;
view view=inflater.inflate(r.layout.producelist, null);
adapter=new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, getdata());
setlistadapter(adapter);
public void onlistitemclick(listview l, view v, int position, long id) {
super.onlistitemclick(l, v, position, id);
mainactivity.java 代碼
import android.app.activity;
import android.app.fragmentmanager;
import android.app.fragmenttransaction;
import android.view.menu;
import android.view.menuitem;
public class mainactivity extends activity {
private fragmentmanager manager;
private fragmenttransaction transaction;
protected void oncreate(bundle savedinstancestate) {
setcontentview(r.layout.activity_main);
manager=getfragmentmanager();
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.main, menu);
return true;
public boolean onoptionsitemselected(menuitem item) {
transaction=manager.begintransaction();
switch (item.getitemid()) {
case r.id.sys:
sysfragment sysfragment=new sysfragment();
//用replace
transaction.replace(r.id.main, sysfragment, "sysfragment");
transaction.addtobackstack("sysfragment");//加入回退棧
break;
case r.id.use:
usefragment usefragment=new usefragment();
transaction.replace(r.id.main, usefragment, "usefragment");
transaction.addtobackstack("usefragment");
case r.id.product:
producfragment producfragment=new producfragment();
transaction.replace(r.id.main, producfragment, "producfragment");
transaction.addtobackstack("producfragment");
default:
transaction.commit();
return super.onoptionsitemselected(item);