Android ActionBar详解(三):ActionBar实现切换Tabs标签
Android 实现 详解 标签 切换 tabs ActionBar
2023-09-11 14:18:00 时间
实现切换Tabs标签;
Activity代码:
- public class ActionBarTabs extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.action_bar_tabs);
- }
- public void onAddTab(View v) {
- final ActionBar bar = getActionBar();
- final int tabCount = bar.getTabCount();
- final String text = "Tab " + tabCount;
- bar.addTab(bar.newTab().setText(text)
- .setTabListener(new TabListener(new TabContentFragment(text))));
- }
- public void onRemoveTab(View v) {
- final ActionBar bar = getActionBar();
- bar.removeTabAt(bar.getTabCount() - 1);
- }
- public void onToggleTabs(View v) {
- final ActionBar bar = getActionBar();
- if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
- bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
- bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
- } else {
- bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
- }
- }
- public void onRemoveAllTabs(View v) {
- getActionBar().removeAllTabs();
- }
- private class TabListener implements ActionBar.TabListener {
- private TabContentFragment mFragment;
- public TabListener(TabContentFragment fragment) {
- mFragment = fragment;
- }
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- ft.add(R.id.fragment_content, mFragment, mFragment.getText());
- }
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- ft.remove(mFragment);
- }
- public void onTabReselected(Tab tab, FragmentTransaction ft) {
- Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
- }
- }
- private class TabContentFragment extends Fragment {
- private String mText;
- public TabContentFragment(String text) {
- mText = text;
- }
- public String getText() {
- return mText;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
- TextView text = (TextView) fragView.findViewById(R.id.text);
- text.setText(mText);
- return fragView;
- }
- }
- }
涉及的布局文件action_bar_tabs.xml代码为:
- < ?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">
- < FrameLayout android:id="@+id/fragment_content"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1" />
- < LinearLayout android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:orientation="vertical">
- < Button android:id="@+id/btn_add_tab"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_add_tab"
- android:onClick="onAddTab" />
- < Button android:id="@+id/btn_remove_tab"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_remove_tab"
- android:onClick="onRemoveTab" />
- < Button android:id="@+id/btn_toggle_tabs"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_toggle_tabs"
- android:onClick="onToggleTabs" />
- < Button android:id="@+id/btn_remove_all_tabs"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_remove_all_tabs"
- android:onClick="onRemoveAllTabs" />
- < /LinearLayout>
- < /LinearLayout>
布局文件action_bar_tab_content.xml;
- < ?xml version="1.0" encoding="utf-8"?>
- < TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
相关文章
- [Android]calabash-android再深入
- android开发中ConditionVariable的典型用法
- Android_(控件)使用ImageView简单实现图片翻转
- Android 开发之旅:短信的收发及在android模拟器之间实践(二)
- 105、android:windowSoftInputMode属性详解
- Android实现推送方式解决方案
- android脚步---使用framelayout实现霓虹灯效果
- Android特效专辑(十一)——仿水波纹流量球进度条控制器,实现高端大气的主流特效
- Android特效专辑(九)——仿微信雷达搜索好友特效,逻辑清晰实现简单
- EasyPlayer实现Android MediaMuxer录像MP4(支持G711/AAC/G726音频)
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
- Android不使用图片框架,实现加载类似微博超长图片的技巧手法;
- android 证书验证流程分析_Android签名机制之---签名验证过程详解
- Android Studio 修改项目包名
- Android 9.0 去除锁屏界面及SystemUI无sim卡拨打紧急电话控件显示功能实现
- android 9.0 SystemUI锁屏解锁界面隐藏导航栏解锁以后显示导航栏功能实现
- Android 圆角EditText的实现和光标的右移
- Android android studio 项目放在源码中编译
- 【Android Gradle 插件】组件化中的 Gradle 构建脚本实现 ⑤ ( 优化 Gradle 构建脚本 | 构建脚本结构 | 闭包定义及用法 | 依赖配置 | android 块配置 )
- 09_android入门_採用android-async-http开源项目的GET方式或POST方式实现登陆案例
- android 常见的解决(mdpi、hdpi 、xhdpi、xxhdpi )屏幕调整
- Android_Fragment_Fragment具体解释
- chromium for android v34 2dcanvas硬件渲染实现分析
- 解决Android SDK Manager下载太慢问题
- Android 四大组件学习之ContentProvider二
- Android开发/源代码资源汇总
- 【Android实战】2、用 CameraX 实现:preview 预览、imageCapture 拍照、videoCapture 录像、videoAnalysis 分析各帧
- Android 解决之依赖包中添加的com.android.support版本不同导致冲突的问题