Setting margins dynamically in code - android

巧了我就是萌 提交于 2019-12-11 05:14:28

问题


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

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