简体   繁体   English

ImageSpan 不适用于 Android 5

[英]ImageSpan not working on Android 5

I have this function that works fine on Android 4.4.1, but breaks on 5.0+.我有这个功能在 Android 4.4.1 上运行良好,但在 5.0+ 上会中断。

  public static SpannableStringBuilder prependImage(Drawable drawable, String text) {
    SpannableStringBuilder builder = new SpannableStringBuilder("  " + text);
    builder.setSpan(new ImageSpan(drawable), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return builder;
  }

And I use it like this:我像这样使用它:

class MyButton extends Button {

    // ... snip ...

    setText(
        prependImage(
            getDrawable(imageResource, color),                     
            getContext().getString(stringResource)),
        BufferType.SPANNABLE);

Here is the getDrawable() method referenced above:这是getDrawable()方法:

 private Drawable getDrawable(int resource, int color) {
    final Resources resources = getContext().getResources();
    Drawable drawable = resources.getDrawable(resource);
    if (drawable != null) {
      drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
      drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    }
    return drawable;
  }

When I debug, everything seems to succeed, but no image is drawn.当我调试时,一切似乎都成功了,但没有绘制图像。 Any ideas what I might be doing wrong?任何想法我可能做错了什么?

By default, in Material buttons are styled to show text in all-caps.默认情况下,材质按钮的样式设置为以全大写显示文本。 However, there is a bug in the AllCapsTransformationMethod used for capitalization that causes it to discard Spannable data.但是,用于大写的 AllCapsTransformationMethod 中存在一个错误,导致它丢弃 Spannable 数据。

You can override the default button styling and disable all-caps by specifying android:textAllCaps="false" on your Button.您可以通过在按钮上指定 android:textAllCaps="false" 来覆盖默认按钮样式并禁用全大写。

<Button
    ...
    android:textAllCaps="false" />

have a look here看看这里

Your code related to working with Spannables is ok.您与使用 Spannables 相关的代码没问题。 You can check it by setting text for TextView.您可以通过为 TextView 设置文本来检查它。

The problem is in material design of button on Android 5.0.问题出在 Android 5.0 上按钮的材料设计中。

<style name="Widget.Material.Button">
    <item name="background">@drawable/btn_default_material</item>
    <item name="textAppearance">?attr/textAppearanceButton</item>
    <item name="minHeight">48dip</item>
    <item name="minWidth">88dip</item>
    <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
    <item name="focusable">true</item>
    <item name="clickable">true</item>
    <item name="gravity">center_vertical|center_horizontal</item>
</style>

There are two solution.有两种解决方法。

The first one is just use TextView as your button and setText with image to it.第一个是使用TextView作为您的按钮,并使用图像设置文本。

For another (and may be more correct) you need to extend button style ( Widget.Material.Button ) in next way:对于另一个(可能更正确),您需要以下一种方式扩展按钮样式( Widget.Material.Button ):

<style name="BtnStyle" parent="android:Widget.Material.Button">
    <item name="android:textAppearance">@null</item>
</style>

Then in your layout:然后在您的布局中:

<Button
    android:id="@+id/test2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Test"
    style="@style/BtnStyle"/>

After you'll do it you should see the images in the button.完成后,您应该会看到按钮中的图像。

Don't forget for Android version that is lower than 5.0 you should create BtnStyle too, but in other resource directory (res/values-v14/style.xml).不要忘记对于低于 5.0 的 Android 版本,您也应该创建BtnStyle ,但在其他资源目录 (res/values-v14/style.xml) 中。

Maybe you need to check the content of that Drawable object, you use the getDrawable() to get the Drawable object, but the API definition seems not match your calling parameters.也许您需要检查该 Drawable 对象的内容,您使用 getDrawable() 获取 Drawable 对象,但 API 定义似乎与您的调用参数不匹配。

For Android 5.0+安卓 5.0+

Drawable getDrawable(int id) This method was deprecated in API level 22. Use getDrawable(int, Theme) instead. Drawable getDrawable(int id) 此方法在 API 级别 22 中已弃用。请改用 getDrawable(int, Theme)。

Drawable getDrawable(int id, Resources.Theme theme) Return a drawable object associated with a particular resource ID and styled for the specified theme. Drawable getDrawable(int id, Resources.Theme theme) 返回与特定资源 ID 关联并为指定主题设置样式的可绘制对象。

The second parameter looks like to be a Theme, not a color.第二个参数看起来像是主题,而不是颜色。 right ?对 ?

try this尝试这个

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getResources().getDrawable(R.drawable.your_drawable, getTheme());
            } else {
                getResources().
                        getDrawable(R.drawable.your_drawable);
            }

需要注意的一件事,可绘制矢量不起作用,您必须有一个可绘制的 png 或 jpeg 或传递给 ImageSpan 位图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM