android - 将可绘制背景设置为 TextView 不起作用

标签 android background textview drawable

我在 StackOverflow 上到处查找,但似乎无法找到我的问题的答案。我正在运行一个 API v.16 设备,下面提供了使用 Drawable 作为其背景的 TextView 的背景更新方法。其他一切正常——TextView 成功更改了它们的 textSize 和高度/宽度,在此处未提及的总代码部分。关于可能是什么问题的任何想法?应用程序不会停止,只是笔划的粗细没有变化。事实上,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> 

最佳答案

试试这个,

Setting background drawable to a TextView dynamically:

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);

Setting background drawable to a TextView using xml:

<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 文件放入您的可绘制文件夹中:

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>

关于android - 将可绘制背景设置为 TextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795890/

相关文章:

Android:TextView 中的多种样式静态

android - 运行 "adb shell wm density 240"更改 Android 设备密度时出错

android - 在应用程序内对 Android 市场上的应用程序进行评分

ios - 信标范围在后台

css - 从 Sprite 中获取图像并将其放入 <td> 标签中

android - TextView anchor 链接空间

android - 从我的位置查找城市名称

java - 升级 android 版本后得到 "Duplicate class android.support.v4.app.INotificationSideChannel"

android - 不显示具有透明背景的应用程序图标

android - 如何在 android 中将一个 TextView 的文本包装在其他 Textview 下?