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时,错误就消失了)
答案 0 :(得分:3)
在Lower API中为VectorDrawable创建一个CustomTextView。
启动自定义Textview的属性,如下所示:
from neo4j.v1 import GraphDatabase
import timeit
start_time = 0
elapsed = 0
task = ""
def startTimer(taskIn):
global task
global start_time
task = taskIn
start_time = timeit.default_timer()
def endTimer():
global task
global start_time
global elapsed
elapsed = round(timeit.default_timer() - start_time, 3)
print(task, elapsed)
startTimer("GraphDatabase.driver")
db = GraphDatabase.driver("bolt://localhost:7687", auth=("USERNAME", "PASSWORD"))
endTimer()
startTimer("db.session")
session = db.session()
endTimer()
startTimer("query1")
result = session.run(
"MATCH (a:User{username:{username}}) RETURN a.username, a.password_hash ",
{"username": "Pingu"})
endTimer()
startTimer("query2")
result = session.run(
"MATCH (a:User{username:{username}}) RETURN a.username, a.password_hash ",
{"username": "Pingu"})
endTimer()
startTimer("db.close")
session.close()
endTimer()
您的自定义Styleable相同。
private void initAttrs(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray attributeArray = context.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
//for font
String fontName = attributeArray.getString(R.styleable.CustomTextView_font);
try {
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(myTypeface);
}
} catch (Exception e) {
e.printStackTrace();
}
Drawable drawableLeft = null;
Drawable drawableRight = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawableLeft = attributeArray.getDrawable(R.styleable.CustomTextView_drawableLeftCompat);
drawableRight = attributeArray.getDrawable(R.styleable.CustomTextView_drawableRightCompat);
drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat);
drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat);
} else {
final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableLeftCompat, -1);
final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableRightCompat, -1);
final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, -1);
final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, -1);
if (drawableLeftId != -1)
drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
if (drawableRightId != -1)
drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
if (drawableBottomId != -1)
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
if (drawableTopId != -1)
drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
}
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
attributeArray.recycle();
}
}