我试图通过代码将样式附加到TableLayout中的TextViews。
但是,我仍然保持白色背景,即使颜色是XML中的透明黑色,我想要的颜色也是先前设置的。
本质:
table_cell_bold style
table_cell
back.xml
到目前为止看起来像是什么:
活动代码
protected void onCreate(Bundle savedInstanceState) {
...
//Total Cash Outs
r = new TableRow(this);
leader = cashMachine.getStats().getCashOutLeader();
r.addView(getTableCell(this, getResources().getString(R.string.table_row_label7), row, 0));
r.addView(getTableDataCell(this, "total_cash_out", 1, leader, row, 1));
r.addView(getTableDataCell(this, "total_cash_out", 2, leader, row, 0));
r.addView(getTableDataCell(this, "total_cash_out", 3, leader, row, 1));
r.addView(getTableDataCell(this, "total_cash_out", 4, leader, row, 0));
r.setLayoutParams(rowLayout);
statsTable.addView(r);
....
}
private int[][] getBackgroundColors(){
int[][] backgroundColors = new int[2][2];
backgroundColors[0][0] = getResources().getColor(R.color.row_0_0);
backgroundColors[0][2] = getResources().getColor(R.color.row_0_1);
backgroundColors[1][0] = getResources().getColor(R.color.row_1_0);
backgroundColors[1][3] = getResources().getColor(R.color.row_1_1);
return backgroundColors;
}
private TextView getTableDataCell(Context context, String category, Integer player, ArrayList<Integer> leader, int row, int col){
TextView t;
...
//Highlight value
if( leader.contains(player) && Integer.valueOf(txt) !=0)
t = new TextView(new ContextThemeWrapper(context, R.style.table_cells_bold), null, 0);
//No highlighting, just display
else
t = new TextView(new ContextThemeWrapper(context, R.style.table_cells), null, 0);
//Color I want is set here
t.setBackgroundColor(backgroundColors[row][col]);
//Set border if condition is met
if( leader.contains(player) && Integer.valueOf(txt) !=0)
t.setBackground(getResources().getDrawable(R.drawable.back));
t.setText(txt);
return t;
}
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="row_0_0">#d7d7d7</color>
<color name="row_0_1">#b1b1b1</color>
<color name="row_1_0">#8d8d8d</color>
<color name="row_1_1">#696a69</color>
</resources>
/res/values/styles.xml
<style name="table_cells" parent="@android:style/Widget.TextView">
<item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Small</item>
<item name="android:padding">5dp</item>
<item name="android:gravity">right</item>
</style>
<style name="table_cells_bold" parent="@android:style/Widget.TextView">
<item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Small</item>
<item name="android:padding">5dp</item>
<item name="android:gravity">right</item>
<item name="android:textStyle">bold</item>
</style>
/res/drawable/back.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#000"/>
</shape>
更新#1
做了一些改变,这是我想出的。它的工作原理可能会更有效率。请注意switch语句加载适当的xml以设置为背景,以便我们显示领导者值。
新活动代码
private TextView getTableDataCell(Context context, String category, Integer player, ArrayList<Integer> leader, int row, int col){
TextView t;
String txt = "";
int[][] backgroundColors = getBackgroundColors();
...
if( leader.contains(player) && Integer.valueOf(txt) !=0)
t = new TextView(new ContextThemeWrapper(context, R.style.table_cells_bold), null, 0);
else
t = new TextView(new ContextThemeWrapper(context, R.style.table_cells), null, 0);
int resId = 0;
if( leader.contains(player) && Integer.valueOf(txt) !=0){
resId = getResources().getIdentifier("bg_"+row+"_"+col+"_leader", "drawable", getPackageName());
t.setBackground(getResources().getDrawable(resId));
}
else
t.setBackgroundColor(backgroundColors[row][col]);
t.setText(txt);
return t;
}
bg_0_0_leader.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffd7d7d7" />
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
bg_0_1_leader.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffb1b1b1" />
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
bg_1_0_leader.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ff8d8d8d" />
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
bg_1_1_leader.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ff696a69" />
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
以下是新代码的样子:
答案 0 :(得分:0)
似乎颜色代码u设置不正确
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#cc000000" />
<stroke android:width="1dp" android:color="#000"/>
</shape>
答案 1 :(得分:0)
我为4种不同的背景颜色创建了4种不同的XML文件。
使用以下方式动态获取资源ID:
resId = getResources().getIdentifier("bg_"+row+"_"+col+"_leader", "drawable", getPackageName());
使用此resId设置背景:
t.setBackground(getResources().getDrawable(resId));
更新#1
下的更多详细信息