AppCompat v22.1.0不是所有的主题化XML部件正确的片段 [英] AppCompat v22.1.0 not theming all xml widgets correctly for fragments

查看:334
本文介绍了AppCompat v22.1.0不是所有的主题化XML部件正确的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用使用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中>
 

的themes.xml

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

解决方案

这是目前报道的错误:<一href="https://$c$c.google.com/p/android/issues/detail?id=169760">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.

这篇关于AppCompat v22.1.0不是所有的主题化XML部件正确的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆