【问题标题】:ColorFilter doesn't work for TextView backgroundColorFilter 不适用于 TextView 背景
【发布时间】:2017-02-13 06:02:44
【问题描述】:

我想使用 ColorFilters 更改几个 TextView 元素背景的颜色。我已经尝试了几种方法来做到这一点。其中两个如下:

TextView tvLeftTop, tvLeftBottom;

tvLeftTop = (TextView) findViewById(R.id.textview_LeftTop);
tvLeftBottom = (TextView) findViewById(R.id.textview_LeftBottom);

float[] cmData = new float[]{
                        1, 0, 0, 1, 0,
                        0, 1, 0, 0, 0,
                        1, 1, 1, 1, 0,
                        0, 0, 0, 1, 0};

ColorMatrix cm = new ColorMatrix(cmData);
ColorFilter filter1 = new ColorMatrixColorFilter(cm);
ColorFilter filter2 = new PorterDuffColorFilter(0x20003300, PorterDuff.Mode.LIGHTEN);

tvLeftBottom.getBackground().setColorFilter(filter1);
tvLeftTop.getBackground().setColorFilter(filter2);

两个 TextView 都包含在 GridLayout 中。这是 .xml 活动文件的相应部分:

<GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:columnCount="4"
        android:rowCount="4"
        android:layout_above="@+id/seekBar">

        <TextView
            android:id="@+id/textview_LeftTop"
            android:layout_columnWeight="1"
            android:layout_rowWeight="2"
            android:layout_rowSpan="2"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_marginBottom="3dp"
            android:layout_marginRight="3dp"
            android:layout_gravity="fill"
            android:background="#6A77B7"

            android:text="Title1" />


        <TextView
            android:id="@+id/textview_LeftBottom"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="2"
            android:layout_rowSpan="2"
            android:layout_row="2"
            android:layout_column="0"
            android:layout_marginTop="3dp"
            android:layout_marginRight="3dp"
            android:layout_gravity="fill"
            android:background="#D64F97"

            android:text="Title2" />
</GridLayout>

但是对两个目标文本视图都没有影响(它们的颜色没有改变)。 我做错了什么?

附:颜色的值是测试的,所以有整个字符串(不是 .xml 文件中的值)。我觉得现在没关系了。

【问题讨论】:

  • 还有其他信息。我尝试使用 SeekBar 更改 TextView 颜色。现在我注意到有趣的事情。我在tvLeftTop.getBackground().setColorFilter(filter2); 之后添加了tvLeftTop.setText(""+i); 并更改了颜色(i 是搜索栏位置的值)。似乎需要刷新 TextView 。但我不知道如何正确地做到这一点。

标签: android background textview background-color colorfilter


【解决方案1】:
tvLeftBottom.setBackgroundColor(Color.argb(0, 255, 255, 255));

您的文本视图没有背景,因此您无法设置颜色。 将您的颜色设置为背景。

【讨论】:

  • TextView 的背景颜色在 android:background 的 .xml 文件中指定。但我还是尝试了你的建议。不幸的是,它不起作用。
猜你喜欢
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2011-06-30
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多