首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ColorFilter不适用于TextView背景

ColorFilter不适用于TextView背景
EN

Stack Overflow用户
提问于 2016-10-05 06:57:28
回答 1查看 762关注 0票数 0

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

代码语言:javascript
复制
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活动文件的适当部分:

代码语言:javascript
复制
<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>

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

P.S. Colors的值是测试的,所以有整个字符串(不是来自.xml文件的值)。我觉得现在已经不重要了。

EN

回答 1

Stack Overflow用户

发布于 2016-10-05 07:10:17

代码语言:javascript
复制
tvLeftBottom.setBackgroundColor(Color.argb(0, 255, 255, 255));

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39863011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档