【发布时间】:2017-08-11 04:56:24
【问题描述】:
TL;DR:在 Textview 上使用简单的 android:drawableLeft 功能会导致崩溃
TextView 是 Android 最基本的视觉元素之一。那为什么它的最基本的功能不起作用呢?
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft
我的代码很简单——它是一个使用TextView 旁边的图像的自定义对话框:
<TextView
android:text="Text next to check mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView19"
android:drawableLeft="@drawable/ic_check_circle_white_24dp"
android:textSize="24sp"
android:textStyle="normal|bold"/>
代码在可视化编辑器上提供了惊人的结果:
但当程序实际执行时,简单的android:drawableLeft 功能会使应用程序崩溃:
android.view.InflateException: Binary XML file line #64: Error inflating class TextView
我尝试像这样打开对话框:
dialog = new Dialog(this,R.style.Theme_AppCompat_Light_Dialog_Alert );
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.premium_dialog);
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setLayout((int) (getResources().getDisplayMetrics().widthPixels * 0.90), (int) (getResources().getDisplayMetrics().heightPixels * 0.90));
dialog.show();
为什么这种基本功能会导致InflateException? (我知道是 drawableLeft 函数导致了问题,因为当我删除 TextView 时,错误消失了)
【问题讨论】:
-
从您的完整堆栈跟踪中,您在哪里使用过
Caused by: android.content.res.Resources$NotFoundException: File **res/drawable/abc_dialog_material_background.xml** from color state list resource ID #0x7f020011 -
你不能直接在
Textview中使用vectordrwable,API较低。创建 CustomTextview 以支持 Lower API 中的矢量
标签: java android xml algorithm text