android - View Gravity not working -
im overlaying linearlayout.the layout contains button.the button should top right of linearlayout.but gravity doesnt seem work.
code: inside oncreate method of service.
final windowmanager.layoutparams params3 = new windowmanager.layoutparams( windowmanager.layoutparams.type_system_alert, windowmanager.layoutparams.flag_not_touch_modal | windowmanager.layoutparams.flag_watch_outside_touch | windowmanager.layoutparams.flag_not_focusable, pixelformat.translucent); linearlayout ll=new linearlayout(this); linearlayout ll2=new linearlayout(this); linearlayout.layoutparams lp=new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content,linearlayout.layoutparams.wrap_content); lp.gravity=gravity.right; lp.width=30; lp.height=30; b=new button(this); b.setbackgroundresource(r.drawable.x); params3.gravity=gravity.top; params3.height=200; params3.width=200; ll.addview(b, lp); wm.addview(ll, params3); the linearlayout 200x200 created , on top.but button isnt top right.i tried using b.setwidth , b.setheight. not help.
linearlayout default horizontal can't align horizontally in horizontal linearlayout (e.g right, center_horizontal, left) , can't align vertically in vertical linearlayout (e.g top center_vertical, bottom) in vertical linearlayout.
if need align right must either set linearlayout vertical or use different viewgroup, example framelayout.
linearlayout ll = new linearlayout(this); ll.setorientation(linarlayout.vertical); ant buttom on top since it's first item. , why not doing in xml? easier when less code.
edit: put button top right of videoview layout this.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <videoview android:id="@+id/videoview1" android:layout_width="200dp" android:layout_height="200dp" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" android:layout_marginright="10dp" android:layout_aligntop="@+id/videoview1" android:layout_alignright="@+id/videoview1" android:text="button" /> </relativelayout> put layout in layout res folder of project. project/res/layout/your_layout.xml
to attach layout activity's window:
public final class youractivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.your_layout); // videoview videoview vv = (videoview) findviewbyid(r.id.videoview1); //get button reference view button = findviewbyid(r.id.button1); } }
Comments
Post a Comment