上一篇文章介紹了ActionBar的使用,這裡介紹ActionBar的另一種使用方法,達到的效果和以前的GroupActivity或TabHost是一樣的,可作為導航來使用。
實作效果圖:
源代碼:
布局檔案:activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@android:color/white">
</RelativeLayout>
f1.xml(體育新聞對應的布局檔案):
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="歡迎收看體育新聞..."
android:textSize="20sp"
android:textColor="@android:color/holo_blue_dark"/>
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
f2.xml(娛樂新聞):
<LinearLayout 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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="歡迎收看娛樂新聞..."
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp" />
</LinearLayout>
f3.xml(軍事新聞):
<LinearLayout 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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="歡迎收看軍事新聞..."
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp" />
</LinearLayout>
代碼檔案:
MainActivity:
package com.fragmentdemo10_actionbar;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
private ActionBar actionBar;
/**
* 設定三個整型常量,分别為0,1,2;分别對應 :運動新聞、娛樂新聞、軍事新聞。
*/
private final int SPORTS = 0;
private final int ENTERTAINMENT = 1;
private final int MILITARY = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
// 設定ActionBar的導航模式
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
/**
* 添加三個tab,分别為:體育新聞,娛樂新聞,軍事新聞。
*/
actionBar.addTab(actionBar.newTab().setText("體育新聞")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(SPORTS));
actionBar.addTab(actionBar.newTab().setText("娛樂新聞")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(ENTERTAINMENT));
actionBar.addTab(actionBar.newTab().setText("軍事新聞")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(MILITARY));
}
class MyTabListener implements TabListener {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (Integer.parseInt(tab.getTag().toString())) {
/**
* 對應體育新聞
*/
case SPORTS:
ft.replace(R.id.main, new FragementA());
break;
/**
* 對應娛樂新聞
*/
case ENTERTAINMENT:
ft.replace(R.id.main, new FragementB());
break;
/**
* 對應軍事新聞
*/
case MILITARY:
ft.replace(R.id.main, new FragementC());
break;
default:
break;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
FragmentA(Tab體育新聞對應的Fragment):
package com.fragmentdemo10_actionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Tab體育新聞對應的Fragment
*
*/
public class FragementA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f1, null);
return view;
}
}
FragmentB(Tab娛樂新聞對應的Fragment):
package com.fragmentdemo10_actionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* TAB娛樂新聞對應的Fragment
*
*/
public class FragementB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f2, null);
return view;
}
}
FragmentC(Tab軍事新聞對應的Fragment):
package com.fragmentdemo10_actionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* TAB軍事新聞對應的Fragment
*
*/
public class FragementC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f3, null);
return view;
}
}
源代碼下載下傳:
點選下載下傳源碼