Android 中ActionBar+fragment实现页面导航的实例
程序员文章站
2023-12-17 10:20:22
android 中actionbar+fragment实现页面导航的实例
为保证android2.0以上均能运行,使用support.v7库下的actionbar及fra...
android 中actionbar+fragment实现页面导航的实例
为保证android2.0以上均能运行,使用support.v7库下的actionbar及fragment
继承自appcompatactivity(actionbaractivity已过时)使用getsupportactionbar()得到actionbar,
actionbar.tab,这里tab必须设置监听,在监听中实现fragment的切换。
这里重点提一下,theme主题一定要适配,因为我使用的是appcompatactivity所以,
android:theme="@style/theme.appcompat.light"
如果不用appcompatactivity一定要注意使用相应的主题适配,否则会getactionbar/getsupportactionbar的时候拿不到东西,空指针报错
<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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <framelayout android:id="@+id/context" android:layout_width="match_parent" android:layout_height="match_parent"/> </relativelayout>
package com.example.yasin.actionbarusing; import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.actionbar; import android.support.v7.app.actionbaractivity; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; public class mainactivity extends appcompatactivity { actionbar actionbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tab tab1 = actionbar.newtab().settext("tab1"); tab1.settablistener(new mytablistener(new fragment1())); actionbar.tab tab2 = actionbar.newtab().settext("tab2"); tab2.settablistener(new mytablistener(new fragment2())); actionbar.addtab(tab1); actionbar.addtab(tab2); } class mytablistener implements actionbar.tablistener{ private fragment fragment; public mytablistener (fragment fragment){ this.fragment=fragment; } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { ft.replace(r.id.context,fragment); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { //ft.remove(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"> <textview android:id="@+id/tv1" android:text="fragment1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
package com.example.yasin.actionbarusing; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; /** * created by yasin on 2016/1/3. */ public class fragment1 extends fragment{ @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment1,container,false); return view; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yasin.actionbarusing" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/theme.appcompat.light" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
效果图:
如有疑问请留言或者到本站社区交流讨论,大家共同进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读