我正在尝试为片段设置主题。

在 list 中设置主题无效:

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

通过查看以前的博客,似乎我不得不使用ContextThemeWrapper。谁能推荐我一个编码示例?我什么都找不到。

最佳答案

在 list 中设置主题通常用于 Activity 。

如果要为Fragment设置主题,请在Fragment的onCreateView()中添加下一个代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.yourLayout, container, false);
}

07-28 04:40