Android actionBar與Fragment結合使用Demo2

來源:互聯網
上載者:User

標籤:android   actionbar fragment   

上一篇文章介紹了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;@Overrideprotected 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 {@Overridepublic 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;}}@Overridepublic void onTabUnselected(Tab tab, FragmentTransaction ft) {}@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.f3, null);return view;}}

原始碼下載:

點擊下載源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.