为Fragment设置主题

132

我正在尝试为一个碎片设置主题。

在清单文件中设置主题无效:

android:theme="@android:style/Theme.Holo.Light"

从以前的博客看来,似乎我必须使用ContextThemeWrapper。有人可以给我一个编码示例吗?我找不到任何东西。

13个回答

0

在你想要不同主题的片段中使用这个:

@Override
    public void onAttach(@NonNull Context context) {
        context.setTheme(R.style.your_them);
        super.onAttach(context);
    }

0

我通过在调用充气机之前在片段上下文中设置主题来使其工作。

注意:这是Xamarin.Android与MvvmCross结合的示例。我不能百分之百确定这也适用于Java程序员。但你可以尝试一下 :)

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    Context.SetTheme(Theme);

    base.OnCreateView(inflater, container, savedInstanceState);

    var view = this.BindingInflate(FragmentLayoutId, container, false);

    // Doing some other stuff

    return view;
}

SetTheme 扩展方法的代码

public static void SetTheme(this Context context, AppThemeStyle themeStyle)
{
    var themeStyleResId = themeStyle == AppThemeStyle.Dark ? Resource.Style.AppTheme : Resource.Style.AppTheme_Light;

    context.SetTheme(themeStyleResId);
}

希望这能帮助到一些人,干杯!


-1

如果你在Lollipop上想尝试这个方法,在onAttach中加入以下代码:

final Window window = activity.getWindow(); window.setStatusBarColor(myStatusBarColor)

然后在onDetach中将其恢复为默认值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接