ColorFilters를 사용하여 여러 TextView 요소 배경의 색을 변경하고 싶습니다. 나는 그것을 할 수있는 몇 가지 방법을 시도했습니다. 그 중 두 개가 울부 짖음 :TextView 배경에 ColorFilter가 작동하지 않습니다.
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. 색상 값은 테스트이므로 전체 문자열 (.xml 파일의 값이 아님)이 있습니다. 나는 그것이 지금 중요하지 않다고 생각한다.
추가 정보가 있습니다. SeekBar를 사용하여 TextView 색상을 변경하려고합니다. 그리고 지금 나는 흥미로운 것을 발견했습니다. 'tvLeftTop.getBackground(). setColorFilter (filter2);와'tvLeftTop.setText (""+ i);'를 추가하고 색상을 변경했습니다 ('i'는 탐색 막대 위치의 값입니다). TextView 새로 고침이 필요합니다. 그러나 나는 그것을 올바르게하는 방법을 모른다. – Yulia