51 Android fragment actionbar 中 Menu 界面管理

strings.xml  文件代码

<?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"/>
    
    <item
        android:id="@+id/use"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/use"/>
    
    <item
        android:id="@+id/product"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/product"/>
    
    

</menu>


sys.xml (加载到 fragment)

<?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>

use.xml (加载到 fragment)

<?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="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)

<?xml version="1.0" encoding="utf-8"?>
 <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:layout_width="match_parent"
    android:layout_height="match_parent"
    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);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view =inflater.inflate(R.layout.sys, null);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

}


 UseFragment extends Fragment

 UseFragment.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 UseFragment extends Fragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.use, null);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

}

ProducFragment extends ListFragment

ProducFragment.java代码

package com.example.android_fragment_menu;

import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
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 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;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.producelist, null);
		adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getData());
		setListAdapter(adapter);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
	

}


MainActivity.java 代码

package com.example.android_fragment_menu;

import android.os.Bundle;
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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		manager=getFragmentManager();
		
	}

	@Override
	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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		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");

			break;
		case R.id.product:
			ProducFragment producFragment=new ProducFragment();
			transaction.replace(R.id.main, producFragment, "producFragment");
			transaction.addToBackStack("producFragment");

			break;

		default:
			break;
		}
		transaction.commit();
		return super.onOptionsItemSelected(item);
	}

}



51 Android fragment actionbar 中 Menu 界面管理_第1张图片




51 Android fragment actionbar 中 Menu 界面管理_第2张图片


51 Android fragment actionbar 中 Menu 界面管理_第3张图片







你可能感兴趣的:(51 Android fragment actionbar 中 Menu 界面管理)