在Fragmet中应用Theme

在一个Fragment中使用了一个ViewPager,每个ViewPager中亦是一个Fragment。形成一个嵌套。

今天碰到一个问题,我要给ViewPager所在的Fragment应用一个主题theme,因为这个Fragment使用了ViewpagerIndicator,我要给其中的TabPageIndicator定制我想要的外观。而看了Demo之后,发现最简单的定制外观的方式就是通过Theme。但是Fragment不是Activity,无法直接在AndroidManifest.xml中写theme,于是在网上google了一下,Fragment应用Theme的方法,最后终于让我找到了,关键代码是这样的:

//使用ContextThemeWrapper通过目标Theme生成一个新的Context
Context ctxWithTheme = new ContextThemeWrapper(getActivity().getApplicationContext(), R.style.Theme_PageIndicatorDefaults);
//通过生成的Context创建一个LayoutInflater
LayoutInflater localLayoutInflater = inflater.cloneInContext(ctxWithTheme);
//使用生成的LayoutInflater创建View
rootView = localLayoutInflater.inflate(R.layout.main, container, false);
使用得到的RootVIew就行了~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值