android - AppCompat v22.1.0 没有为 fragment 正确设置所有 xml 小部件的主题

标签 android android-fragments material-design android-appcompat

当使用 AppCompat 22.1.0 使用基于 xml 的布局时,并非所有支持的小部件都为使用 Android 4.4 的 fragment 着色或 Material 主题。

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

  • RadioButton(无色调)
  • 复选框(无色调)
  • 微调器(应用设备默认主题)
  • EditText(应用设备默认主题)
  • RatingBar(应用设备默认主题)
  • 按钮(应用设备默认主题)

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

截图(左4.4,右5.0):

Example screenshot

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 小部件。

关于android - AppCompat v22.1.0 没有为 fragment 正确设置所有 xml 小部件的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806343/

相关文章:

javascript - 适用于 android 的 React-native 阴影 Prop

创建 Activity 时,Android 键盘会自动出现在 EditText 中

android - 边距属性在自定义 DialogFragment 中不起作用

javascript - 单击时防止在特定条件下打开 md-select 下拉菜单

c# - Dragablz TabablzControl可见性属性不起作用

android - Fling on ListView - 什么方向?

带有标签部分的 Android ListView

android - getSupportFragmentManager() - 类型 Context 未定义

android - fragment View 通过主视图的 id 查找 View

android - Cardview不可去除的灰色下划线(Android 4.0.4设备)