Har*_*ani 0 svg android android-support-library android-5.0-lollipop android-vectordrawable
我在我的 Android 项目中使用了 svg 文件。Android 4.4 或更低版本存在问题。我已经尝试过这些解决方案
app:srcCompat
,vectorDrawables.useSupportLibrary = true
在 gradle 文件和 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
在BaseActivity
.不要使用以上 3 种解决方案,只需将您的替换ImageView
为android.support.v7.widget.AppCompatImageView
. 无需做任何额外的努力。
注: - TextView
,EditText
和其它类,它使用drawableRight和drawableLeft不被支持。对于他们,使用TextView or EditText
和创建您自己的复合视图类AppCompatImageView
在FrameLayout
或 中RelativeLayout
。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>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppEditTextWithDrawable">
<attr name="drawableRightSVG" format="reference" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1266 次 |
最近记录: |