【问题标题】:AppCompat Material Style widget tint errorAppCompat 材质样式小部件色调错误
【发布时间】:2020-02-02 15:59:46
【问题描述】:

我正在使用 appcompat_v7 库。

在文档中说:

[...] AppCompat 在早期版本的 Android 上为 UI 小部件子集提供了类似的行为:

AppCompat 工具栏提供的一切(动作模式等)

  • 编辑文本
  • 微调器
  • 复选框
  • 单选按钮
  • Switch(使用新的 android.support.v7.widget.SwitchCompat)
  • CheckedTextView 您不需要做任何特别的事情来使这些工作,只需像往常一样在您的布局中使用这些控件,AppCompat 将完成其余的工作(有一些警告;请参阅下面的常见问题解答)。

这是我的代码:

值/样式.xml

<style name="AppBaseTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/material_blue_grey_800</item>
    <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
    <item name="colorAccent">@color/material_deep_teal_200</item>
</style>

布局/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lollipoptest.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:text="CheckBox" />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox1"
    android:layout_centerHorizontal="true"
    android:text="RadioButton" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioButton1"
    android:layout_centerHorizontal="true"
    android:ems="10" >

    <requestFocus />
</EditText>

默认 MainActivity.java 没有变化

操作栏显示新的 Material 样式。编辑文本也是。 但复选框和单选按钮不是。 我做错了什么?

照片: https://drive.google.com/file/d/0B1jx6rK5CDqTRnhYVDU3V21JRlk/view?usp=sharing

【问题讨论】:

  • 截图是哪个SDK级别的?我使用完全相同的东西,Checkbox 样式在 Jellybean 设备中发生了变化。
  • 2.2,我也试过 2.3。

标签: android android-appcompat


【解决方案1】:

我找到了解决办法:

 <CheckBox
        android:id="@+id/chbDevuelto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:button="@drawable/abc_btn_check_material"
        android:buttonTint="@color/colorAccent"
        android:layout_marginStart="16dp"
        android:focusable="false" />

这条线可以解决问题:

android:button="@drawable/abc_btn_check_material" android:buttonTint="@color/colorAccent"

适用于所有版本:3

【讨论】:

  • 很奇怪,但确实android:button="@drawable/abc_btn_check_material" 行用appcompat-v7:21.0.(2|3) 修复了缺失的着色。
  • 如何给 buttonTint ?
【解决方案2】:

我认为它不适用于这些版本。它模拟棒棒糖中使用的色调。在低于 4.0 的版本中,单选按钮和复选框只是图像。这很难着色。 Appcompat 对它可以着色的内容进行着色。试试 4.0 版(冰淇淋三明治),从那个版本开始它可能会工作。

看看这个,这是 Chris Banes 写的一篇关于 Appcompat v21 的文章。 https://chris.banes.me/2014/10/17/appcompat-v21/

您可以使用 4.0 作为最低 SDK 版本。您有大约 85% 的设备支持 4.0。它还可以为您省去很多麻烦。以下是更多信息的链接:https://developer.android.com/about/dashboards/index.html

【讨论】:

  • 你是对的。我认为总体上是 85%。并且超过 95% 在活跃用户中。
  • @FedeAmura 你可以做的是为那些低版本创建一个自定义单选按钮和复选框。这是一个可以帮助您的链接:icetea09.com/blog/2014/01/22/…
【解决方案3】:

这是 AppCompat r22 中的一个错误,一些小部件(例如 ButtonCheckboxRadioButton)没有使用 Android 2.2/2.3 中的类材质背景。

它已在 AppCompat r22.1 中修复。

简而言之,不再需要手动将abc_btn_check_material 和类似资源设置为这些小部件的背景可绘制资源;图书馆会自动完成。

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2022-08-18
    • 2019-01-17
    • 2021-08-07
    • 2018-10-08
    相关资源
    最近更新 更多