How to set the absolute position of the image or button so that it is consistent in different screen sizes?

After searching for several hours, I could not find the exact answer to my situation. I am currently using RelativeLayout, and all I have is a background image and a button. The problem I am facing is to place the button in the place where I want it to be (a slight offset from the center).

My first attempt was to change the layout_margins of a button. While this worked for the current emulator I was working with (3.7in WVGA), the button positioning was slightly / removed for a different screen size (e.g. 3.2in HVGA).

Then I tried changing the parent layout padding, but got the same problem. My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background">
    <Button android:id="@+id/starttimer" 
            android:background="@drawable/button" 
            android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/>
</RelativeLayout>

, , . , , , , . !

+3
2

, , layout_centerHorizontal, .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background">
    <Button android:id="@+id/starttimer" 
            android:background="@drawable/button" 
            android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android_marginLeft="5dp"/>
</RelativeLayout>
+2

.

0

Source: https://habr.com/ru/post/1778912/


All Articles