В моем Android-приложении есть таблица с семью столбцами. Я хочу изменить цвет фона этих столбцов при обновлении содержимого в таблице. Как указать столбцы в main.xml и как изменить цвет фона? Благодарю.
Отредактировано main.xml...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#CDCDCD" android:orientation="vertical">
<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="100" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"></TextView>
<TextView android:text="100" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2"></TextView>
<TextView android:text="100" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3"></TextView>
<TextView android:text="100" android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4"></TextView>
<TextView android:text="100" android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="5"></TextView>
<TextView android:text="100" android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="6"></TextView>
<TextView android:text="100" android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="7"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="100" android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"></TextView>
<TextView android:text="100" android:id="@+id/textView12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2"></TextView>
<TextView android:text="100" android:id="@+id/textView13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3"></TextView>
<TextView android:text="100" android:id="@+id/textView14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4"></TextView>
<TextView android:text="100" android:id="@+id/textView15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="5"></TextView>
<TextView android:text="100" android:id="@+id/textView16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="6"></TextView>
<TextView android:text="100" android:id="@+id/textView17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="7"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
РЕДАКТИРОВАТЬ:
Это пример того, почему я хочу выделить весь столбец вместо отдельных текстовых просмотров. Когда я выделяю отдельные текстовые изображения, они имеют разные размеры, поэтому они не расширяются до ширины самого широкого текстового поля в этом столбце. Если никто не знает, как изменить размер моих текстовых представлений, чтобы соответствовать размеру самого широкого текстового поля в каждом столбце. Если кто-нибудь это знает, напишите.
EDIT: результаты добавления свойств layout_weight в тег TableRow...
Я также попробовал то же самое, но вместо этого добавил свойство layout_weight вместо тега TextView. Вот результат...
Чтобы дать вашему текстуру равную ширину и, следовательно, устранить проблему с высокой степенью освещенности, вы должны посмотреть на свойство layout_weight
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CDCDCD"
android:orientation="vertical">
<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<TextView android:text="100" android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="1"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<TextView android:text="100" android:id="@+id/textView11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="1"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
Теперь я не на своем компьютере разработки, поэтому не могу проверить это (и исправить мое форматирование), но он должен работать, я проверю его позже.
Я закончил использование серии вложенных линейных макетов, полноэкранного горизонтального с равномерно распределенными вертикальными для столбцов. Если я затем установил прозрачность фона TextView, я могу изменить весь фон столбца, установив фон столбца LinearLayout.
Возможно, это не самое приятное решение, но вы можете изменить цвет фона всех текстовых элементов в столбце.
TextView textview2 = (TextView) findViewById(R.id.textView2);
TextView textview11 = (TextView) findViewById(R.id.textView11);
textview2.setBackgroundColor(Color.RED);
textview11.setBackgroundColor(Color.RED);