【问题标题】:AppCompat v22.1.0 not theming all xml widgets correctly for fragmentsAppCompat v22.1.0 没有为片段正确主题化所有 xml 小部件
【发布时间】:2015-06-30 15:05:57
【问题描述】:

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

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

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

它曾经在 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>

【问题讨论】:

  • 确实!来自 Google 的快速响应!

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


【解决方案1】:

这目前被报告为错误: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 替换每个小部件。

【讨论】:

  • 哎哟。很高兴仍然在 v22 上。
  • @Rolf 如果你发现了,你能把它标记为正确吗?
  • 对于 5.x 之前的版本,即使使用 AppCompat 23.0.1,我也看到了与上面屏幕截图中相同的效果。解决方法没有区别。在我的情况下,我有一个 AppCompatActivity,其中包含在 xml 中定义的 ListFragment。 ListFragment 使用默认的列表布局。它的突出显示颜色是全息蓝色,而不是我的主题的 colorAccent。另一个列表片段包含一个 CheckedTextView,其复选框不支持主题的强调色。
  • 有趣的是,我在 具有 列表视图但不是 ListFragment 的片段的列表项中获得了正确颜色的复选框。
  • 奇怪我也使用 ListFragment(没有 xml,因为 ListFragment 不需要它)并且它按预期工作。我应该提到 appcompat 不支持 Material 主题列表选择器。所以你应该自己解决这些问题:motzcod.es/post/108837945597/…
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2015-06-29
    • 2015-06-30
    • 2019-09-13
    相关资源
    最近更新 更多