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

Android AppCompat v22.1.0没有为片段正确设置所有xml小部件的主题,android,android-fragments,material-design,android-appcompat,Android,Android Fragments,Material Design,Android Appcompat,当使用AppCompat 22.1.0使用基于xml的布局时,并非所有受支持的小部件都使用Android 4.4为我的片段着色或材料主题 我在以下小部件中看到了这种行为(其他未经测试): RadioButton(无色调) 复选框(无着色颜色) 微调器(应用设备默认主题) EditText(应用设备默认主题) 分级栏(应用设备默认主题) 按钮(应用了设备默认主题) 它曾经在AppCompat v22.0.0中工作 屏幕截图(左4.4,右5.0): public class MainActivi

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

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

  • RadioButton(无色调)
  • 复选框(无着色颜色)
  • 微调器(应用设备默认主题)
  • EditText(应用设备默认主题)
  • 分级栏(应用设备默认主题)
  • 按钮(应用了设备默认主题)
它曾经在AppCompat v22.0.0中工作

屏幕截图(左4.4,右5.0):

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;
        }
    }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_main, container, false);
    return rootView;
}

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;
        }
    }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = getActivity().getLayoutInflater().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>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
</resources>

Themes.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>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
</resources>

这是当前报告的错误:

临时解决方法是使用片段父活动LayoutFlater:
getActivity().GetLayoutFlater()
,而不是onCreateView方法中提供的LayoutFlater

示例:

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;
        }
    }
}
@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>


然后,线性布局中的所有视图都采用清单(通过主题)中相应活动声明的强调色。

Ouch。很高兴仍然在v22上。@Rolf如果你弄明白了,你能将其标记为正确吗?我看到的效果与上面5.x之前版本的屏幕截图中的效果相同,即使使用AppCompat 23.0.1。解决办法没有什么区别。在我的例子中,我有一个AppCompatActivity,它包含一个用xml定义的ListFragment。ListFragment使用默认的列表布局。它的突出颜色是全息蓝,而不是我的主题的颜色。另一个列表片段包含CheckedTextView,其复选框不支持主题的强调颜色。有趣的是,我在具有列表视图但不是ListFragment的片段的列表项中获得了正确颜色的复选框。奇怪的是,我也使用ListFragment(ListFragment不需要xml)它的工作原理与预期一致。我应该提到,appcompat不支持以材质为主题的列表选择器。所以你应该自己解决这些问题:真的!来自谷歌的快速响应!