问题
I have an activity which contains only ScrollView and a TableLayout in it. In my code i'm adding tablerows, everything works fine except margins - it's not working (nothing happens) Here is the code:
LayoutParams rowparams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
rowparams.setMargins(10, 10, 10, 10);
for (int i = 0; i < obiekty + 1; i++) {
rowparams.setMargins(10, 10, 10, 10);
trs[i] = new TableRow(MainActivity.this);
trs[i].setGravity(Gravity.CENTER_VERTICAL);
trs[i].setLayoutParams(rowparams);
trs[i].setBackgroundColor(Color.parseColor("#33B5E5"));
}
tvs[licznik].setText(nazwa);
tvs[licznik].setBackgroundColor(Color.parseColor("#000000"));
tvs[licznik].setGravity(Gravity.CENTER_VERTICAL);
iv[licznik].setImageResource(obraz);
iv[licznik].setBackgroundColor(Color.parseColor("#FFBB33"));
trs[licznik].addView(tvs[licznik]);
trs[licznik].addView(iv[licznik]);
tl.addView(trs[licznik]);
Thanks for help
回答1:
use TableLayout.LayoutParams
TableLayout.LayoutParams rowparams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
来源:https://stackoverflow.com/questions/12826067/setting-margins-dynamically-in-code-android