AppCompat v22.1.0 没有正确地为片段设置所有 xml 小部件的主题

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

当使用 AppCompat 22.1.0 使用基于 xml 的布局时,并非所有支持的小部件都被着色或 material 为我使用 Android 4.4 的片段设置主题。

我在以下小部件(其他未测试)中看到了这种行为:

它曾经在 AppCompat v22.0.0 中工作。

截图(左4.4,右5.0):

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

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

fragment_main.xml

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton test"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox test"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/someStrings"/>
</LinearLayout>

Themes.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
</resources>

当前报告为错误:https://code.google.com/p/android/issues/detail?id=169760

临时解决方法是使用 Fragment 父级 Activity LayoutInflater:getActivity().getLayoutInflater() 而不是 onCreateView 方法中提供的 LayoutInflater。

示例:

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

注意:另一种解决方案是在 xml 布局中使用特殊的 AppCompat 小部件:

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

但这基本上意味着您需要将每个小部件替换为 AppCompat 小部件。

你可以强制将主题应用于视图,只需要在父视图上添加这些行:

<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>

然后线性布局内的所有视图都采用在清单(通过主题)中声明为各自 activity 的强调色。