【发布时间】:2016-02-21 02:57:27
【问题描述】:
我在 StackOverflow 上到处查找它,但似乎找不到我的问题的答案。我正在运行一个 API v.16 设备,下面提供了使用 Drawable 作为其背景的 TextView 的背景更新方法。其他一切正常——TextViews 成功地改变了它们的 textSize 和 height/width,在此处未提及的总代码部分中。关于可能是什么问题的任何想法?应用程序不会停止,只是笔画的粗细没有变化。事实上,TextView 在更改中完全失去了它的背景。它的原始背景是一个具有一定笔画宽度的圆角平滑矩形,其大小应减半,以及它的笔画宽度。更改后,TextView 中不再显示背景。
if (textViewsArrayList.size() != 0) textViews.get(textViewsArrayList.size() - 1).post(new Runnable() {
@Override
public void run() {
for (counter = 0; counter < textViewsArrayList.size(); counter++) {
textViewsArrayList.get(counter).getLayoutParams().height = (int)
(textViewsArrayList.get(counter).getLayoutParams().height / 2);
textViewsArrayList.get(counter).getLayoutParams().width = (int) (textViewsArrayList.get(counter).getLayoutParams().width / 2);
((TextView) textViewsArrayList.get(counter)).setTextSize(TypedValue.COMPLEX_UNIT_PX, (int) (((TextView) textViewsArrayList.get(counter)).getTextSize() / 2));
GradientDrawable background = (GradientDrawable) textViewsArrayList.get(counter).getBackground();
background.setStroke((int) (4 / displayMetrics.density / 2), (int) Color.parseColor("#FFA500"));;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((TextView) textViewsArrayList.get(counter)).setBackground((Drawable) background);
} else {
((TextView) textViewsArrayList.get(counter)).setBackgroundDrawable((Drawable) background);
}
}
}
});
虽然相关 TextView 的 xml 是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="320dp"
android:layout_height="90dp"
android:tag="layout">
<TextView
android:id="@+id/textview"
android:layout_height="68dp"
android:layout_width="match_parent"
android:background="@drawable/drawable_xml"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="7dp"
android:tag="textview_in question"/>
etc.
至于可绘制的xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="4dp"
android:color="#FF6600" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<solid android:color="#FFFFFF" />
</shape>
【问题讨论】:
-
从您的 xml 中删除 android:background="@drawable/drawable_xml"。仅从 java 代码中设置背景
-
嘿,当我检查了你的代码并且我也回答了你的问题时,你的代码是完美的。
-
@Anjali:试过了,还是不行。还是谢谢。
-
这可能是绘制视图可绘制对象时的问题吗?有人有这方面的专业知识吗?
标签: android background textview drawable