Изменить фон у нескольких блоков
У меня есть n блоков RelativeLayout. Каким образом всем им изменить фон программно?
Есть RelativeLayout созданные программно. Их разное количество, от 3 до 10-ти. По клику нужно поменять background у всех созданных RelativeLayout.
<RelativeLayout
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9A9595"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Текст"
android:textSize="10dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9A9595"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Текст"
android:textSize="10dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/idN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9A9595"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Текст"
android:textSize="10dp"
/>
</RelativeLayout>
Ответы (1 шт):
Автор решения: Style-7
→ Ссылка
Берете родителя этих RelativeLayout(s) и проходите в цикле
RelativeLayout rl_root = findViewById( R.id.root ); // здесь ваш, тип может быть другой
for( int i = 0; i < rl_root.getChildCount(); i++ ){
View v = rl_root.getChildAt( i );
v.setBackgroundColor( Color.CYAN );
}