【问题标题】:SVG/VectorDrawable issue in androidAndroid 中的 SVG/VectorDrawable 问题
【发布时间】:2016-12-08 07:30:46
【问题描述】:

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

  1. app:srcCompat,
  2. vectorDrawables.useSupportLibrary = true 在 gradle 文件中和
  3. AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);BaseActivity 的静态块中。
除了gridview,应用程序中不显示图像

【问题讨论】:

  • 您的“问题”是什么?如果不知道出了什么问题,就很难为您提供帮助。
  • @PaulLeBeau 检查最后一行,除了gridview,应用程序中不显示图像。
  • 不要在 Android 上使用 SVG 浪费时间。我向你保证,无论你从设计师那里得到什么 SVG,你无法直接使用它们,因为 Android 不支持 很多 SVG 功能。因此,paintcodeapp.com 的流行度
  • 我刚刚检查了 www.paintcodeapp.com 并在 Our Customers 下显示 GOOGLE !!这证实了 Android 中的 SVG 处理很糟糕!

标签: android svg android-5.0-lollipop android-support-library android-vectordrawable


【解决方案1】:

不要使用上述 3 种解决方案,只需将您的 ImageView 替换为 android.support.v7.widget.AppCompatImageView。无需做任何额外的努力。 注意:- 不支持TextViewEditText 等使用drawableRight 和drawableLeft 的类。对于他们,在FrameLayoutRelativeLayout 中使用TextView or EditTextAppCompatImageView 创建您自己的复合视图类。 drawableRight 内部 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>

【讨论】:

    【解决方案2】:

    SVG 文件转换为XML 并从Drawable 文件夹中使用它。检查this

    【讨论】:

    • Android Studio 有这个功能。右键单击drawable 文件夹-> 选择“新建”-> 选择“矢量资产”并选择 svg 文件。它将被转换成 VectorDrawable xml 文件。
    • 目的是我们也可以使用 SVG 文件,因为 XML 在所有 android 版本中都可以正常工作。
    猜你喜欢
    • 2023-03-21
    • 2021-10-26
    • 2016-06-27
    • 2020-10-20
    • 1970-01-01
    • 2017-12-10
    • 2015-01-09
    • 2022-11-17
    • 2015-02-02
    相关资源
    最近更新 更多