AppCompat v22.1.0不是所有的主题化XML部件正确的片段有的、部件、片段、正确

2023-09-05 00:22:07 作者:伴你余生

在使用使用AppCompat 22.1.0基于XML的布局并非所有的支持部件浅淡或材料的主题,使用Android 4.4系统的我的片段。

我看到这种行为具有以下部件(其它未测试):

单选按钮(无色彩的颜色) 复选框(无色彩的颜色) 微调(设备默认主题应用) 的EditText(设备默认主题应用) 的RatingBar(设备默认主题应用) 按钮(设备默认主题应用)

它用在AppCompat v22.0.0工作。

截图(左4.4,右5.0):

MainActivity.java:

 公共类MainActivity扩展AppCompatActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        如果(savedInstanceState == NULL){
            getSupportFragmentManager()
                    .beginTransaction()
                    。新增(R.id.container,新PlaceholderFragment())
                    。承诺();
        }
    }

    公共静态类PlaceholderFragment扩展片段{

        公共PlaceholderFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
            返回rootView;
        }
    }
}
 

fragment_main.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:方向=垂直>

    <单选按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:检查=真
        机器人:文本=单选按钮测试/>

    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:检查=真
        机器人:文本=复选框测试/>

    <微调
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:项=@阵列/ someStrings/>
< / LinearLayout中>
 
中兴Mifavor桌面app下载 Mifavor Launcher3.0 中兴Mifavor桌面 安卓版 1.0 2016032517 极光下载站

的themes.xml

 <资源>
    <样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>< /风格>
< /资源>
 

解决方案

这是目前报道的错误:https://$c$c.google.com/p/android/issues/detail?id=169760

一个临时的解决办法是使用的片段父活动LayoutInflater: getActivity()getLayoutInflater()而不是提供LayoutInflater在onCreateView方法

示例:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    查看rootView = getActivity()getLayoutInflater()膨胀(R.layout.fragment_main,集装箱,FALSE)。;
    返回rootView;
}
 

注:另一种方法是使用特殊的AppCompat部件在你的XML布局:的

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

但是,这将基本上意味着你需要更换的AppCompat 1的每一个部件。的

When using xml based layouts using AppCompat 22.1.0 not all supported widgets are tinted or material themed for my Fragments using Android 4.4.

I see this behavior with the following widgets (others not tested):

RadioButton (No tint color) CheckBox (No tint color) Spinner (Device default theme is applied) EditText (Device default theme is applied) RatingBar (Device default theme is applied) Button (Device default theme is applied)

It used to work in AppCompat v22.0.0.

Screenshot (left 4.4, right 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>

解决方案

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.