我在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.
至于drawable 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>
答案 0 :(得分:1)
试试这个,
动态设置背景可绘制到TextView:
TextView txtHello = new TextView(MainActivity.this);
txtHello.setLayoutParams(lparams);
txtHello.setText("Hello World");
txtHello.setTextSize(14);
txtHello.setTextColor(Color.parseColor("#9C9C9C"));
txtHello.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_launcher, null, null, null);
使用xml将背景绘制为TextView:
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="7dp"
android:background="@drawable/rounded_corner"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="16sp" />
将以下xml文件放入drawable文件夹:
<强> rounded_corner.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:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<solid android:color="#FFFFFF" />
</shape>