AppCompat v22.1.0 not theming all xml widgets correctly for fragments

天大地大妈咪最大 提交于 2019-12-03 00:07:24

This is currently reported as bug: https://code.google.com/p/android/issues/detail?id=169760

A temporary workaround is to use the Fragment parent Activity LayoutInflater: getActivity().getLayoutInflater() instead of the supplied LayoutInflater in the onCreateView method.

Example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_main, container, false);
    return rootView;
}

Note: Another solution is to use the special AppCompat widgets in your xml layout:

  • android.support.v7.widget.AppCompatRadioButton
  • android.support.v7.widget.AppCompatCheckBox
  • android.support.v7.widget.AppCompatSpinner

But this would basically mean you need to replace every single widget with the AppCompat one.

You can force the theme applied to views, only need to add these lines on the parent view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:tools="http://schemas.android.com/tools"
           tools:context="com.example.yourActivityThatHasATheme"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">
</LinearLayout>

Then all views inside the linear layout take the accent color declared to the respective activity in the manifest (Via theme).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!