android中的SVG/VectorDrawable问题 [英] SVG/VectorDrawable issue in android

查看:91
本文介绍了android中的SVG/VectorDrawable问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android项目中使用了svg文件. Android 4.4或更低版本中存在问题.我已经在gradle文件中尝试了这些解决方案

I have used svg files in my Android project. There are issues in Android 4.4 or lower versions. I have tried these solutions

  1. app:srcCompat
  2. vectorDrawables.useSupportLibrary = true,并在BaseActivity的静态块中尝试了
  3. AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);.
  1. app:srcCompat,
  2. vectorDrawables.useSupportLibrary = true in gradle file and
  3. AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in static block of BaseActivity.

除了gridview之外,图像未显示在应用中.

Other than gridview, images are not shown in app.

推荐答案

只需使用android.support.v7.widget.AppCompatImageView代替ImageView,而不是使用上述3种解决方案.无需做任何额外的努力. 注意:-:不支持使用drawableRight和drawableLeft的TextViewEditText和其他类.对于它们,在FrameLayoutRelativeLayout中使用TextView or EditTextAppCompatImageView创建您自己的复合视图类. EditTextdrawableRight的示例:-

Instead of using above 3 solutions, just replace your ImageView with android.support.v7.widget.AppCompatImageView. No need to do any extra effort. Note:- TextView, EditText and other classes, which use drawableRight and drawableLeft are not supported. For them, create your own compound view class with TextView or EditText and AppCompatImageView in a FrameLayout or RelativeLayout. Example of drawableRight inside EditText:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edt_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLines="1"
        android:paddingEnd="40dp"
        android:paddingLeft="5dp"
        android:paddingRight="40dp"
        android:paddingStart="5dp" />

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/search"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right|center_vertical"
        android:layout_margin="8dp"
        android:src="@drawable/ic_svg_search" />
</FrameLayout>

代码

public class EditTextWithDrawable extends FrameLayout {
    public AppCompatImageView mDrawableRight;
    public EditText mAppEditText;
    public AppEditTextWithDrawable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    private void init(AttributeSet attrs) {
        if (attrs != null && !isInEditMode()) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.compound_view, this, true);
            mDrawableRight = (AppCompatImageView) ((FrameLayout) getChildAt(0)).getChildAt(1);
            mAppEditText = (EditText) ((FrameLayout) getChildAt(0)).getChildAt(0);

            TypedArray attributeArray = getContext().obtainStyledAttributes(
                    attrs,
                    R.styleable.EditTextWithDrawable);

            int drawableRes =
                    attributeArray.getResourceId(
                            R.styleable.EditTextWithDrawable_drawableRightSVG, -1);
            if (drawableRes != -1) {
                mDrawableRight.setImageResource(drawableRes);
            }
            attributeArray.recycle();
        }
    }
}

attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AppEditTextWithDrawable">
        <attr name="drawableRightSVG" format="reference" />
    </declare-styleable>
</resources>

这篇关于android中的SVG/VectorDrawable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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