简体   繁体   中英

How can i make an app to fit all screen sizes using relative layout?

How can i make an app to fit all screen sizes using relative layout? At the moment i am using constant values for the button positions and sizes and it looks good only on HTC One S. That's the device i am using for development. I want the buttons to be on the appropriate positions on every display.

I have one background in the app, that's the HUD you can see it here:

平视显示器

The buttons and the LED must be in the exact locations inside the holes. And it looks like this:

机器人

The layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hud"
android:orientation="horizontal"
android:splitMotionEvents="false"
android:windowEnableSplitTouch="false"
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/connect_button"
    android:layout_width="122sp"
    android:layout_height="122sp"
    android:layout_marginLeft="338dp"
    android:layout_marginTop="54dp"
    android:background="@drawable/connect_inactive"
    android:contentDescription="@string/connect_button_description" />

<ImageButton
    android:id="@+id/disconnect_button"
    android:layout_width="122sp"
    android:layout_height="122sp"
    android:layout_marginLeft="338dp"
    android:layout_marginTop="195dp"
    android:background="@drawable/disconnect_inactive"
    android:contentDescription="@string/disconnect_button_description" />

<ImageButton
    android:id="@+id/throttleup_button"
    android:layout_width="122sp"
    android:layout_height="122sp"
    android:layout_marginLeft="483dp"
    android:layout_marginTop="54dp"
    android:background="@drawable/throttleup_inactive"
    android:contentDescription="@string/throttleup_button_description" />

<ImageButton
    android:id="@+id/throttledown_button"
    android:layout_width="122sp"
    android:layout_height="122sp"
    android:layout_marginLeft="483dp"
    android:layout_marginTop="195dp"
    android:background="@drawable/throttledown_inactive"
    android:contentDescription="@string/throttledown_button_description" />

<ImageButton
    android:id="@+id/moveforward_button"
    android:layout_width="170sp"
    android:layout_height="160sp"
    android:layout_marginLeft="79dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/control_forward_inactive"
    android:contentDescription="@string/moveforward_button_description" />

<ImageButton
    android:id="@+id/movebackward_button"
    android:layout_width="170sp"
    android:layout_height="160sp"
    android:layout_marginLeft="79dp"
    android:layout_marginTop="153dp"
    android:background="@drawable/control_backward_inactive"
    android:contentDescription="@string/movebackward_button_description" />

<ImageButton
    android:id="@+id/moveleft_button"
    android:layout_width="170sp"
    android:layout_height="160sp"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="92dp"
    android:background="@drawable/control_left_inactive"
    android:contentDescription="@string/moveleft_button_description" />

<ImageButton
    android:id="@+id/moveright_button"
    android:layout_width="170sp"
    android:layout_height="160sp"
    android:layout_marginLeft="141dp"
    android:layout_marginTop="92dp"
    android:background="@drawable/control_right_inactive"
    android:contentDescription="@string/moveright_button_description" />

<ImageView
    android:id="@+id/led"
    android:layout_width="45sp"
    android:layout_height="45sp"
    android:layout_marginLeft="297dp"
    android:layout_marginTop="280dp"
    android:background="@drawable/led_inactive"
    android:contentDescription="@string/led_description" />

</RelativeLayout>

Instead of hard coding the values, use the dimen files for this purpose. Specify different folders for values,like values-large etc, and create dimension files and put specific values according to screen size.

Use weight for all Layouts instead of giving constant values, Because weight is a type of % for screen and in manifest by default support screen view is true.

Create a Multiple Screen size layout folders like this

layout-small , layout-large , layout-xlarge so u copy your layout XML files in to these folder and now u can edit the specified width and size of the boxes .

These links will help you to create application for multiple screens :

  1. http://developer.android.com/training/multiscreen/screensizes.html

  2. http://developer.android.com/guide/practices/screens_support.html

These are android's official documents on implementing support for multiple screen sizes.

http://developer.android.com/guide/practices/screens_support.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM