android - Placing Button on top of ImageView that stretches with different screen sizes -
i have image (match_parent) 2 rectangles in containing options. trying place 2 transparent buttons on top of image clicking on image results in action. however, trying support multiple screen sizes, while able play around layout margins line rectangles in buttons specific resolution smartphone, testing tablet failed.
how place buttons consistently line image stretches fill varying screen sizes.
pretty simple layout code right using dp doesn't work
<imageview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/banner" android:background="@drawable/banner" android:contentdescription="@string/banner" /> <button android:layout_width="120dp" android:layout_height="90dp" android:id="@+id/vs_computer" android:layout_margintop="135dp" android:layout_marginleft="135dp" android:alpha="0" android:clickable="true" android:shadowcolor="@android:color/transparent" android:singleline="true" /> <button android:layout_width="120dp" android:layout_height="100dp" android:id="@+id/multiplayer" android:layout_margintop="260dp" android:layout_marginleft="135dp" android:alpha="0" android:clickable="true" android:shadowcolor="@android:color/transparent" android:singleline="true" />
the temporary image using: options http://ft.trillian.im/17107e0bd3d38d3147acf89ff7b55b6543455af6/6ypgztjzurzbt6uacyiwsqvjotojc.jpg
you omit 2 button graphics background image (just leave blank space) , make them separate images. can put on 2 imagebutton
s. solves immediate problem of having line absolutely pixel perfectly.
to size , position buttons, figure out left/right , top/bottom margins percentage of screen width , height. can create custom layout (subclass of viewgroup
) , implement onlayout(...)
method position 2 buttons within desired region of background image applying percentages whatever view size happens be.
that custom viewgroup this:
public class blah extends viewgroup { button b1; @override protected void onfinishinflate() { super.onfinishinflate(); b1 = (button) findviewbyid(r.id.button_1); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { // in dimensions xml: <fraction name="button_margin_horizontal">20%p</fraction> int left = (int) getresources().getfraction(r.fraction.button_margin_horizontal, 0, getwidth()); int right = getwidth() - left; int buttonheight = b1.getmeasuredheight(); b1.layout(left, 0, right, buttonheight); } }
Comments
Post a Comment