解决FragmentTabHost切换标题栏变更问题

本文介绍如何在使用FragmentTabHost布局时,为每个Fragment设置自定义的标题栏,通过定义第一个Fragment的自定义布局并传递给ActionBar,实现每个Fragment具有不同的标题栏。同时提供第二个Fragment的实现,将自定义布局设置为空或重新设置,以适应不同需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在都流行FragmentTabHost布局.但是所有的fragment都是共享一个actionbar,但是我们又想给每个fragment定义自定义的标题栏.百度google了好久也没有找到解决方案.最后想到了一个笨办法.解决方法如下.如果你们有更好的方案,请留言.


第一个fragment中我定义了自定义布局的actionbar

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		initActionBar();
		layoutView = inflater.inflate(R.layout.fragment1, null);
		return layoutView;
	}

	private void initActionBar() {
		ActionBar actionBar=getActivity().getActionBar();
		actionBar.setTitle("第一个页面");
		//查找布局文件   并且为按钮添加点击事件
		View view=View.inflate(mActivity, R.layout.action_title, null);
        actionBar.setDisplayShowCustomEnabled(true);//可设置自定义布局
        actionBar.setCustomView(view);
	}


第二个fragment把自定义布局设置为空,或者你也可以重新设置一个自定义布局的actionbar,这样就可以实现每个fragment都有不同的actionbar.

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		initActionBar();
		layoutView = inflater.inflate(R.layout.fragment2, null);
		return layoutView;
	}
	
	private void initActionBar() {
		ActionBar actionBar=getActivity().getActionBar();
		actionBar.setTitle("第二个页面");
		actionBar.setCustomView(null);
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安辉就是我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值