Android编程之ActionBar Tabs用法实例分析
作者:Flying_tao 时间:2021-11-08 08:49:26
本文实例讲述了Android编程之ActionBar Tabs用法。分享给大家供大家参考,具体如下:
这里主要实现用Tab切换不同的Fragment,点击View显示or隐藏ActionBar,把ActionBar 设为透明,使界面更加友好,详细代码见资源里的ActionBarTabs。
ActionBar Tab主要用于Fragment之间的切换,其必须要设置ActionBar.TabListener,详细代码如下
ActionBarActivity.Java:
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.Window;
public class ActionBarActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//使ActionBar变得透明
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// remove the activity title to make space for tabs
actionBar.setDisplayShowTitleEnabled(false);
AFragment aFragment = new AFragment();
actionBar.addTab(actionBar.newTab().setText("Tab-A")
.setTabListener(new ListenerA(aFragment)));
BFragment bFragment = new BFragment();
actionBar.addTab(actionBar.newTab().setText("Tab-B")
.setTabListener(new ListenerB(bFragment)));
}
//点击显示or隐藏ActionBar
public boolean onTouchEvent(MotionEvent event){
ActionBar bar = getActionBar();
switch(event.getAction()){
case MotionEvent.ACTION_UP:
if(bar.isShowing()) bar.hide();
else bar.show();
break;
default:
break;
}
return true;
}
private class ListenerA implements ActionBar.TabListener {
private AFragment mFragment;
// Called to create an instance of the listener when adding a new tab
public ListenerA(AFragment fragment) {
mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment, mFragment, null);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing }
}
}
private class ListenerB implements ActionBar.TabListener {
private BFragment mFragment;
// Called to create an instance of the listener when adding a new tab
public ListenerB(BFragment fragment) {
mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment, mFragment, null);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing }
}
}
}
其中涉及到两个Fragment,在前面Fragment的笔记中讲过,这里就不再赘述。类AFragment实现如下,BFragment实现与这类似:
public class AFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.alayout, container, false);
}
}
希望本文所述对大家Android程序设计有所帮助。
标签:Android,ActionBar,Tabs
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ProgressDialog使用总结
2023-07-10 22:16:33
Android自定义StickinessView粘性滑动效果
2022-11-22 08:56:20
![](https://img.aspxhome.com/file/2023/9/90179_0s.gif)
C#使用文件流读取文件的方法
2022-06-27 20:17:12
c#添加图片、文本水印到PDF文件
2021-08-30 13:36:57
![](https://img.aspxhome.com/file/2023/0/111210_0s.png)
Spring MVC如何使用@RequestParam注解获取参数
2021-05-28 14:20:19
![](https://img.aspxhome.com/file/2023/4/83584_0s.png)
java设计模式学习之工厂方法模式
2023-10-12 17:19:04
![](https://img.aspxhome.com/file/2023/7/58607_0s.jpg)
mybatis 多表关联mapper文件写法操作
2021-12-02 23:21:25
Java关于MyBatis缓存详解
2021-11-01 00:40:20
Android通过自定义View实现随机验证码
2022-10-24 07:16:19
![](https://img.aspxhome.com/file/2023/7/104657_0s.png)
c# winform时钟的实现代码
2023-04-05 07:40:53
浅析C# 9.0 新特性之 Lambda 弃元参数
2022-05-08 16:28:22
![](https://img.aspxhome.com/file/2023/9/119569_0s.png)
Android学习之Broadcast的简单使用
2023-11-19 15:18:02
java并发JUC工具包AtomicInteger原子整型语法基础
2023-10-05 14:16:47
Java Spring AOP之PointCut案例详解
2023-05-24 16:46:15
Java实现在Word指定位置插入分页符
2021-06-29 03:24:44
![](https://img.aspxhome.com/file/2023/1/75451_0s.png)
SpringDataMongoDB多文档事务的实现
2023-11-25 06:37:55
java定义二维数组的几种写法(小结)
2021-07-05 11:52:09
Java实现两人五子棋游戏(五) 判断是否有一方胜出
2022-03-03 18:29:44
![](https://img.aspxhome.com/file/2023/5/64955_0s.jpg)
SpringBoot中的响应式web应用详解
2022-05-01 05:47:49
![](https://img.aspxhome.com/file/2023/3/132183_0s.png)
SpringBoot项目jar和war打包部署方式详解
2023-05-10 14:58:29