How do I change the background color for a column of my Android app

不想你离开。 提交于 2020-01-15 20:12:22

问题


My Android app has a table with seven columns. I'd like to change the background color of these columns when the content in the table is updated. How do I specify columns in the main.xml and how do I change their background color? Thanks.

Edit main.xml posted...

<?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:

This is an example of why I want to highlight the entire column instead of the individual textviews. When I highlight the individual textviews they are different sizes so they do not expand to the width of the widest textview in that column. Unless anyone knows how to change the size of my text views to fit the size of the widest textview in each column. If anyone does know this, please post.

EDIT: Results of adding layout_weight properties to the TableRow tag...

I also tried the same thing but added the layout_weight property to the TextView tag instead. Here is the result...


回答1:


To give your textviews equal width and therefore fix your higlighting issue you should look at the layout_weight property

 <?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>

Now I'm not at my development PC so can't verify this (and fix my formatting), but it should work I'll check it later.




回答2:


Maybe not the prettiest solution but you could change the background color of all the TextViews in the column.

TextView textview2 = (TextView) findViewById(R.id.textView2);
TextView textview11 = (TextView) findViewById(R.id.textView11);
textview2.setBackgroundColor(Color.RED);
textview11.setBackgroundColor(Color.RED);



回答3:


I ended up using a series of nested linear layouts, a full-screen horizonal one with evenly spaced vertical ones for the columns. If I then set the TextView background to transparent, I can change the entire column background by setting the background of the column's LinearLayout.



来源:https://stackoverflow.com/questions/6641103/how-do-i-change-the-background-color-for-a-column-of-my-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!