Java 是否可以使用android:DrawableRight在按钮和文本视图中使用VectorDrawable?
当我在textview或imageview中使用VectorDrawable资源时,当使用“android:DrawableRight”/“android:DrawableEnd”/“android:DrawableStart”/“android:DrawableLeft”时,我会遇到运行时崩溃 应用程序将在没有任何警告的情况下正常编译 我正在使用Java 是否可以使用android:DrawableRight在按钮和文本视图中使用VectorDrawable?,java,android,android-layout,android-vectordrawable,compound-drawables,Java,Android,Android Layout,Android Vectordrawable,Compound Drawables,当我在textview或imageview中使用VectorDrawable资源时,当使用“android:DrawableRight”/“android:DrawableEnd”/“android:DrawableStart”/“android:DrawableLeft”时,我会遇到运行时崩溃 应用程序将在没有任何警告的情况下正常编译 我正在使用 梯度1.5 支持库23.2('com.android.Support:appcompat-v7:23.2.0') 但我发现,我可以在Java中以
- 梯度1.5
- 支持库23.2('com.android.Support:appcompat-v7:23.2.0')
TextView tv = (TextView) findViewById(R.id.textView);
tv.setCompoundDrawablesWithIntrinsicBounds(null,null, getResources().getDrawable(R.drawable.ic_accessible_white_36px),null);
(我怀疑这是23.2的支持库错误。)
但是有可能对SVG资产使用drawableRight等吗
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<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="au.com.angryitguy.testsvg.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_accessible_white_36px"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="Hello World!"/>
</RelativeLayout>
<TextView
drawableSvgRight="@{@drawable/svg_ic_battle_trophy}"
.....
这是谷歌材料设计网站上未经修改的矢量绘图资产
<vector android:height="24dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M12,4m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
<path android:fillColor="#FFFFFF" android:pathData="M19,13v-2c-1.54,0.02 -3.09,-0.75 -4.07,-1.83l-1.29,-1.43c-0.17,-0.19 -0.38,-0.34 -0.61,-0.45 -0.01,0 -0.01,-0.01 -0.02,-0.01L13,7.28c-0.35,-0.2 -0.75,-0.3 -1.19,-0.26C10.76,7.11 10,8.04 10,9.09L10,15c0,1.1 0.9,2 2,2h5v5h2v-5.5c0,-1.1 -0.9,-2 -2,-2h-3v-3.45c1.29,1.07 3.25,1.94 5,1.95zM12.83,18c-0.41,1.16 -1.52,2 -2.83,2 -1.66,0 -3,-1.34 -3,-3 0,-1.31 0.84,-2.41 2,-2.83L9,12.1c-2.28,0.46 -4,2.48 -4,4.9 0,2.76 2.24,5 5,5 2.42,0 4.44,-1.72 4.9,-4h-2.07z"/>
</vector>
这是坠机事件。(请注意引用textview的充气错误。)
java.lang.RuntimeException:无法启动活动组件信息{
au.com.angryitguy.testsvg/au.com.angryitguy.testsvg.MainActivity}:
android.view.InflateException:二进制XML文件行#13:
膨胀类TextView时出错
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
在android.app.ActivityThread.access$600(ActivityThread.java:130)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
位于android.os.Handler.dispatchMessage(Handler.java:99)
位于android.os.Looper.loop(Looper.java:137)
位于android.app.ActivityThread.main(ActivityThread.java:4745)
...
原因:android.view.InflateException:
二进制XML文件行#13:膨胀类TextView时出错
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
在android.view.LayoutInflater.充气(LayoutInflater.java:489)
在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
在android.view.LayoutInflater.充气(LayoutInflater.java:352)
在android.support.v7.app.AppCompateDelegateImplV7.setContentView(AppCompateDelegateImplV7.java:267)
位于android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
位于au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14)
在android.app.Activity.performCreate上(Activity.java:5008)
...
原因:android.content.res.Resources$NotFoundException:
文件res/drawable/ic_accessible_white_36px.xml来自可绘制资源ID#0x7f02004b
位于android.content.res.Resources.loadDrawable(Resources.java:1918)
位于android.content.res.TypedArray.getDrawable(TypedArray.java:601)
位于android.widget.TextView。(TextView.java:622)
位于android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:60)
位于android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:56)
在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)中
在android.support.v7.app.AppCompateDelegateImplV7.createView(AppCompateDelegateImplV7.java:963)
位于android.support.v7.app.AppCompateDelegateImplv7.onCreateView(AppCompateDelegateImplv7.java:1022)
位于android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
在android.view.LayoutInflater.充气(LayoutInflater.java:489)
在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
在android.view.LayoutInflater.充气(LayoutInflater.java:352)
在android.support.v7.app.AppCompateDelegateImplV7.setContentView(AppCompateDelegateImplV7.java:267)
位于android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
位于au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14)
在android.app.Activity.performCreate上(Activity.java:5008)
...
原因:org.xmlpull.v1.XmlPullParserException:
二进制XML文件行#1:无效的可绘制标记向量
位于android.graphics.drawable.drawable.createFromXmlInner(drawable.java:877)
位于android.graphics.drawable.drawable.createFromXml(drawable.java:818)
位于android.content.res.Resources.loadDrawable(Resources.java:1915)
位于android.content.res.TypedArray.getDrawable(TypedArray.java:601)
位于android.widget.TextView。(TextView.java:622)
位于android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:60)
位于android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:56)
在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)中
在android.support.v7.app.AppCompateDelegateImplV7.createView(AppCompateDelegateImplV7.java:963)
位于android.support.v7.app.AppCompateDelegateImplv7.onCreateView(AppCompateDelegateImplv7.java:1022)
位于android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
在android.view.LayoutInflater.充气(LayoutInflater.java:489)
在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
在android.view.LayoutInflater.充气(LayoutInflater.java:352)
在android.support.v7.app.AppCompateDelegateImplV7.setContentView(AppCompateDelegateImplV7.java:267)
位于android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
位于au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14)
在android.app.Activity.performCreate上(Activity.java:5008)
...
此解决方案不再正确。从23.3.0版本开始,只能通过app:srcCompat或setImageResource()加载矢量绘图表。
尝试将可绘制向量包装到图层列表或选择器中:
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_accessible_white_wrapped"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="Hello World!"/>
ic_accessible_white_wrapped.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_accessible_white_36px"/>
</layer-list>
来自谷歌:从Android支持库23.3.0开始,支持向量机
只能通过app:srcCompat或setImageResource()加载drawables
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_accessible_white_wrapped"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="Hello World!"/>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_accessible_white_36px"/>
</layer-list>
<TextView
...
android:drawableRight="@{@drawable/ic_accessible_white_36px}"/>
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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="au.com.angryitguy.testsvg.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:drawableRight="@{@drawable/ic_accessible_white_36px}"
android:text="Hello World!"
android:textColor="#FFFFFF"
android:textSize="22sp"/>
</RelativeLayout>
</layout>
android {
....
defaultConfig {
dataBinding {
enabled = true
}
}
}
android {
....
defaultConfig {
vectorDrawables.useSupportLibrary = true
dataBinding {
enabled = true
}
}
}
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
app:drawableLeftCompat
app:drawableStartCompat
<resources>
<declare-styleable name="CustomTextView">
<attr name="drawableStartCompat" format="reference"/>
<attr name="drawableEndCompat" format="reference"/>
<attr name="drawableTopCompat" format="reference"/>
<attr name="drawableBottomCompat" format="reference"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
}
void initAttrs(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomTextView);
Drawable drawableStart = null;
Drawable drawableEnd = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawableStart = attributeArray.getDrawable(R.styleable.CustomTextView_drawableStartCompat);
drawableEnd = attributeArray.getDrawable(R.styleable.CustomTextView_drawableEndCompat);
drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat);
drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat);
} else {
final int drawableStartId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableStartCompat, -1);
final int drawableEndId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableEndCompat, -1);
final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, -1);
final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, -1);
if (drawableStartId != -1)
drawableStart = AppCompatResources.getDrawable(context, drawableStartId);
if (drawableEndId != -1)
drawableEnd = AppCompatResources.getDrawable(context, drawableEndId);
if (drawableBottomId != -1)
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
if (drawableTopId != -1)
drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
}
// to support rtl
setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
attributeArray.recycle();
}
}
}
<YOUR_VIEW_PACKAGE.CustomTextView
android:id="@+id/edt_my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:drawableStartCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableEndCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableTopCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableBottomCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
/>
android:drawableLeft="@{@drawable/vector_ic_access_time_24px}"
android:drawableStart="@{@drawable/vector_ic_access_time_24px}"
Drawable leftDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_search);
search.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
Drawable drawableTop = AppCompatResources.getDrawable(view.getContext(), iconId);
view.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop, null, null);
TextView titleTextView = (TextView) viewHolder.getView(android.R.id.text1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Drawable leftDrawable = AppCompatResources
.getDrawable(context, R.drawable.ic_tickbox);
titleTextView.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
}
else
{
//Safely create our VectorDrawable on pre-L android versions.
Drawable leftDrawable = VectorDrawableCompat
.create(context.getResources(), R.drawable.ic_tickbox, null);
titleTextView.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
}
<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
app:compoundDrawableHeight="24dp"
app:compoundDrawableWidth="24dp"
app:drawableTopVector="@drawable/some_vector_drawble"
app:drawableEndVector="@drawable/another_vector_drawable"
app:drawableTint="@color/colorAccent" />
compile 'com.tolstykh.textviewrichdrawable:textview-rich-drawable:0.3.2'
android {
compileSdkVersion 26
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
}
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/passName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/account_drawableleft_selector"
android:drawablePadding="5dp"
android:ellipsize="marquee"
android:fontFamily="@font/montserrat_light_family"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:paddingRight="10dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@color/app_text_color"
android:textSize="12sp"
tools:text="Account Name" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_account_circle_24dp" /> <!-- checked -->
</selector>
android {
compileSdkVersion 26
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
dataBinding {
enabled = true
}
}
public class BindingUtils {
@BindingAdapter("android:drawableRight")
public static void setDrawableStart(TextView textView, int resourceId) {
Drawable drawable = AppCompatResources.getDrawable(textView.getContext(), resourceId);
Drawable[] drawables = textView.getCompoundDrawables();
textView.setCompoundDrawablesWithIntrinsicBounds(drawable,
drawables[1], drawables[2], drawables[3]);
}
}
android:drawableRight="@{viewModel.ResId}"
android:drawableRight="@{@drawable/ic_login_24dp}"
android:drawableTop
android:drawableBottom
etc
Drawable drawableTop = null;
...
setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
public class CustomTextView extends AppCompatTextView {
private static final int NOT_SET = -1;
private static final int LEFT = 0;
private static final int START = 0;
private static final int TOP = 1;
private static final int RIGHT = 2;
private static final int END = 2;
private static final int BOTTOM = 3;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
}
void initAttrs(Context context, AttributeSet attrs) {
if (attrs == null) {
return;
}
Drawable[] drawablesArr = getCompoundDrawables();
TypedArray attributeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
Drawable drawableStart = null;
Drawable drawableEnd = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
Drawable drawableLeft = null;
Drawable drawableRight = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawableStart = attributeArray.getDrawable(R.styleable.CustomTextView_drawableStartCompat);
drawableEnd = attributeArray.getDrawable(R.styleable.CustomTextView_drawableEndCompat);
drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat);
drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat);
drawableLeft = attributeArray.getDrawable(R.styleable.CustomTextView_drawableLeftCompat);
drawableRight = attributeArray.getDrawable(R.styleable.CustomTextView_drawableRightCompat);
} else {
final int drawableStartId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableStartCompat, NOT_SET);
final int drawableEndId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableEndCompat, NOT_SET);
final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, NOT_SET);
final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, NOT_SET);
final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableLeftCompat, NOT_SET);
final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableRightCompat, NOT_SET);
if (drawableStartId != NOT_SET)
drawableStart = AppCompatResources.getDrawable(context, drawableStartId);
if (drawableLeftId != NOT_SET)
drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
if (drawableEndId != NOT_SET)
drawableEnd = AppCompatResources.getDrawable(context, drawableEndId);
if (drawableRightId != NOT_SET)
drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
if (drawableBottomId != NOT_SET)
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
if (drawableTopId != NOT_SET)
drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
}
drawableStart = (drawableStart != null ? drawableStart : drawablesArr[START]);
drawableLeft = (drawableLeft != null ? drawableLeft : drawablesArr[LEFT]);
drawableStart = (drawableStart != null ? drawableStart : drawableLeft);
drawableEnd = (drawableEnd != null ? drawableEnd : drawablesArr[END]);
drawableRight = (drawableRight != null ? drawableRight : drawablesArr[RIGHT]);
drawableEnd = (drawableEnd != null ? drawableEnd : drawableRight);
drawableBottom = (drawableBottom != null ? drawableBottom : drawablesArr[BOTTOM]);
drawableTop = (drawableTop != null ? drawableTop : drawablesArr[TOP]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
} else {
setCompoundDrawables(drawableStart, drawableTop, drawableEnd, drawableBottom);
}
attributeArray.recycle();
}
}
val drawable1 = VectorDrawableCompat.create(resources, R.drawable.ic_rb_username, theme)
yourView.setCompoundDrawablesRelativeWithIntrinsicBounds( drawable1, null, null, null)
Drawable drawable1 = VectorDrawableCompat.create(getResources(), R.drawable.ic_rb_username, getTheme());
yourView.setCompoundDrawablesRelativeWithIntrinsicBounds( drawable1, null, null, null);
app:drawableLeftCompat
app:drawableStartCompat
...
TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView, left, top, right, bottom)
@JvmStatic
@BindingAdapter("drawableSvgLeft")
fun addDrawableSvgLeft(textView: TextView,drawable: Drawable){
textView.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
}
@JvmStatic
@BindingAdapter("drawableSvgRight")
fun addDrawableSvgRight(textView: TextView,drawable: Drawable){
textView.setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null)
}
<TextView
drawableSvgRight="@{@drawable/svg_ic_battle_trophy}"
.....