android - Center elements in LinearLayout after creating it programmatically -
i'm creating linearlayout on runtime in i'm adding 2 elements: textview , spinner. center them horizontally in linearlayout, cannot figure out how it. below, code use create views:
linearlayout leftsideattributlayout = new linearlayout(this); leftsideattributlayout.setorientation(linearlayout.horizontal); linearlayout.layoutparams attributlayoutparams = new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content); leftsideattributlayout.setlayoutparams(attributlayoutparams); linearlayout.layoutparams leftlabelparams = new linearlayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); leftlabelparams.gravity = gravity.center; leftattributlabel.setlayoutparams(leftlabelparams); textview leftattributlabel = new textview(this); leftattributlabel.settext(attribut.getnom()); leftattributlabel.settextcolor(color.white); tools.applyfont(getapplicationcontext(), leftattributlabel, "gothic_0.ttf"); linearlayout.layoutparams spinnerparams = new linearlayout.layoutparams(layoutparams.wrap_content,layoutparams.wrap_content); spinnerparams.gravity = gravity.center_horizontal; spinnerparams.setmargins(10, 0, 0, 0); spinner leftattributvalues = new spinner(this); leftattributvalues.setlayoutparams(spinnerparams); leftattributvalues.setadapter(adapter); leftattributvalues.setbackgroundresource(r.drawable.lenti_attributspinner); leftattributvalues.settag(attribut); would great if me ! :)
because have set gravity linearlayout put line after setting linear layout parameter
leftsideattributlayout.setgravity(gravity.center); here sample code
linearlayout leftsideattributlayout = new linearlayout(this); leftsideattributlayout.setorientation(linearlayout.horizontal); linearlayout.layoutparams attributlayoutparams = new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.wrap_content); attributlayoutparams.gravity = gravity.center; leftsideattributlayout.setlayoutparams(attributlayoutparams); leftsideattributlayout.setgravity(gravity.center); linearlayout.layoutparams leftlabelparams = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); //leftlabelparams.gravity = gravity.center; textview leftattributlabel = new textview(this); leftattributlabel.settext("sample"); leftattributlabel.settextsize(25); leftattributlabel.setlayoutparams(leftlabelparams); linearlayout.layoutparams spinnerparams = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content,linearlayout.layoutparams.wrap_content); // spinnerparams.gravity = gravity.center; spinnerparams.setmargins(10, 0, 0, 0); spinner leftattributvalues = new spinner(this); leftattributvalues.setlayoutparams(spinnerparams); leftsideattributlayout.addview(leftattributlabel); leftsideattributlayout.addview(leftattributvalues); mainview.addview(leftsideattributlayout); hope helps
Comments
Post a Comment