![](/img/trans.png)
[英]How do I change the background color of my android app based on user input?
[英]How do I change the background color for a column of my Android app
我的 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>
編輯:
這是為什么我要突出顯示整個列而不是單個文本視圖的示例。 當我突出顯示各個文本視圖時,它們的大小不同,因此它們不會擴展到該列中最寬的 textview 的寬度。 除非有人知道如何更改我的文本視圖的大小以適應每列中最寬的 textview 的大小。 如果有人知道這個,請發帖。
編輯:將 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>
現在我不在我的開發 PC 上,所以無法驗證這一點(並修復我的格式),但它應該可以工作,我稍后會檢查它。
我最終使用了一系列嵌套的線性布局,一個全屏水平布局,列的垂直布局均勻分布。 如果我然后將 TextView 背景設置為透明,我可以通過設置列的 LinearLayout 的背景來更改整個列背景。
也許不是最漂亮的解決方案,但您可以更改列中所有 TextViews 的背景顏色。
TextView textview2 = (TextView) findViewById(R.id.textView2);
TextView textview11 = (TextView) findViewById(R.id.textView11);
textview2.setBackgroundColor(Color.RED);
textview11.setBackgroundColor(Color.RED);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.