2016-10-04 59 views
0

我想使用ColorFilters更改几个TextView元素背景的颜色。我尝试了几种方法来做到这一点。其中两个是波纹管:ColorFilter不适用于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> 

但有两个目标文本的意见没有影响(它们的颜色没有变化)。 我做错了什么?

P.S.颜色的值是测试,所以有整个字符串(不是来自.xml文件的值)。我认为现在并不重要。

+0

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

回答

0
tvLeftBottom.setBackgroundColor(Color.argb(0, 255, 255, 255)); 

你的textview没有背景,所以你不能设置颜色。 将您的颜色设置为背景。

+0

TextView的背景色在android:background的.xml文件中指定。但我仍然尝试了你的建议。不幸的是,这并不奏效。 – Yulia