This example describes the use of code creation layouts for Android programming. Share to everyone for your reference, specific as follows:
Describe the effect: the outermost layer is a relativelayout with a custom linearlayout, each linearlayout has two textview. That ' s it!!!
private void Initview () {//get XML relativelayout layout = (relativelayout) Findviewbyid (R.id.liner); for (int i = 0; i < 5; i++) {Relativelayout.layoutparams relativeparams = new Relativelayout.layoutparams (Vie
WGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Each row has a linearlayout linearlayout llayout = new LinearLayout (mcontext);
Llayout.setid (i + 10);
Llayout.setorientation (linearlayout.horizontal); Linearlayout.layoutparams llayoutlayoutparams = new Linearlayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Llayout.setlayoutparams (Llayoutlayoutparams);
TextView TV = new TextView (mcontext);
Tv.setid (i);
Tv.settext ("This is the first" + i + "text box");
Tv.settextcolor (Color.Blue);
TextView tv_num = new TextView (mcontext);
Tv_num.setid (i + 20);
Tv_num.settext (i + "Taiwan");
Tv_num.settextcolor (color.red); Add a long height setting for textview linearlayout.layoutparams layoutparams_txt = NEW linearlayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Tv.setlayoutparams (Layoutparams_txt);
Tv_num.setlayoutparams (Layoutparams_txt);
Add to the LinearLayout in each row llayout.addview (TV);
Llayout.addview (Tv_num);
Each linearlayout is under the previous one, the first at the top, not the IF (i > 0) {relativeparams.addrule (relativelayout.below, i + 10-1);
//Add each linearlayout to the Relativelayout Layout.addview (Llayout, relativeparams);
}
}
I hope this article will help you with your Android programming.