android - setLayoutParams does not display things correctly -
when i'm setting layout parameters xml, works fine. when i'm trying set layout parameters programmatically, works wrong. did failed?
i need set parameters in linearlayout, here is:
<linearlayout android:id="@+id/buttons_layout" android:layout_width="231dp" android:layout_height="40dp" android:layout_margintop="40dp" android:layout_marginright="85dp" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:orientation="horizontal" android:baselinealigned="true" android:weightsum="603" >
and here code set params:
rl = (linearlayout)findviewbyid(r.id.buttons_layout); lp = new relativelayout.layoutparams(pixelsfromdp(231), pixelsfromdp(40)); lp.addrule(relativelayout.align_parent_right); lp.addrule(relativelayout.align_parent_top); lp.setmargins(0, pixelsfromdp(40), pixelsfromdp(85), 0); rl.setlayoutparams(lp);
here i'm getting setting parameters xml:
and here i'm getting setting parameters programmatically:
method pixelsfromdp returns int value:
public int pixelsfromdp(int pixels){ return (int)(40 * getresources().getdisplaymetrics().density + 0.5f); }
the reason of using method in layoutparams values in pixels, need values in dp, in methos i'm converting dp pixels depends on screen density
oh shi, found mistake! omg i'm stupid :d
probled solved replacing "40" "pixels" in method pixelsfromdp
// try work fine me. thinks there problem in pixelsfromdp()
linearlayout rl = (linearlayout)findviewbyid(r.id.buttons_layout); layoutparams lp = new relativelayout.layoutparams(231, 40); lp.addrule(relativelayout.align_parent_right); lp.addrule(relativelayout.align_parent_top); lp.setmargins(0, 40, 85, 0); rl.setbackgroundcolor(color.dkgray); rl.setlayoutparams(lp);
Comments
Post a Comment