【问题标题】:Setting background drawable to a TextView is not working将背景可绘制对象设置为 TextView 不起作用
【发布时间】: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


【解决方案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>

【讨论】:

  • 谢谢,但我想要的是动态设置它,因为它是为了调整不同设备的布局,我只是看不到它的发生,我可以为应用程序,那将是灾难性的。
  • 你好,我已经更新了代码,请看一下,我已经在我的项目中完成了它,它工作正常,所以请检查一下。
  • @Parth:谢谢,您的回复帮我解决了问题。它暗示我的视野范围可能有问题。事实上,我的 textview 有 match_parent 而不是其 layout_width 的固定值。我猜这是导致问题的原因,可能与系统请求列表视图项(match_parent)的父级大小以处理背景可绘制的更改的时间有关。我更改父级大小(列表视图项的大小)的事实可能是问题所在,因为这可能在系统最终确定新值之前发生。
  • 如果这对你有帮助,请点赞我的回答。
  • 我很抱歉,但赞成一个答案意味着它是正确的,你的只是不小心帮我找到了正确的答案,它实际上并不包含正确的答案。
猜你喜欢
  • 2017-07-05
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多