package com.xys.actionbar;
import Android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AFragmentTab extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
布局:
activity_main.xml:相当于容器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
fragment_a.xml:每个fragment的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_a"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
显示效果如图:
九-1、下拉模式
前面讲到了
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
这次我们使用下拉模式
public class ActionBarTestActivity extends Activity {
/** Called when the activity is first created. */
Button button1,button2;
ActionBar actionBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
actionBar=getActionBar();
// 生成一个SpinnerAdapter
SpinnerAdapter adapter = ArrayAdapter.createFromResource(this, R.array.student, android.R.layout.simple_spinner_dropdown_item);
// 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// 为ActionBar设置下拉菜单和监听器
actionBar.setListNavigationCallbacks(adapter, new DropDownListenser());
class DropDownListenser implements OnNavigationListener
{
// 得到和SpinnerAdapter里一致的字符数组
String[] listNames = getResources().getStringArray(R.array.student);
/* 当选择下拉菜单项的时候,将Activity中的内容置换为对应的Fragment */
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
// 生成自定的Fragment
Ftest student = new Ftest();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
// 将Activity中的内容替换成对应选择的Fragment
transaction.replace(android.R.id.content, student, listNames[itemPosition]);
transaction.commit();
return true;
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string>Hello World, ActionBarTestActivity!</string>
<string>ActionBarTest</string>
<string>ActionBar下拉菜单</string>
<string-array>
<item>item1</item>
<item>item2</item>
<item>item3</item>
<item>item4</item>
</string-array>
</resources>