简体   繁体   中英

How to make android app to fit for all screen sizes

Well i have activity.xml i which i have a background image and a button which also has a background image in it run well on normal screen but when i run it on xlarge or large screen the position of button changes

<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"
    tools:context=".MainActivity"
    android:background="@drawable/menu"
    android:layout_margin="@dimen/my_view_margin"
     >

    <LinearLayout
        android:layout_width="300px"
        android:layout_height="200px"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="172dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="60px"
            android:background="@drawable/bt"
            android:text="Button" />
    </LinearLayout>

</RelativeLayout>

this is the code above and below are the images of different screen

cant add images cause doesnt have enough reputation

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

and if any times you think, It's not possible than add dimens values {folder contains dimens.xml}

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.

<supports-screens android:smallScreens="true" 
          android:normalScreens="true" 
          android:largeScreens="true"
          android:xlargeScreens="true"
          android:anyDensity="true" />

Add this to your manifest file of app.

<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:layout_margin="@dimen/my_view_margin"
    android:background="@drawable/menu"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginTop="170dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/bt"
        android:text="Button" />

</RelativeLayout>

Use Relative layouts, draw 9 images. And for high quality apps you may create different different layout XMLS for all 4 screen sizes. http://developer.android.com/guide/practices/screens_support.html Use this link for more info.

Simple, use relative layout with the set margin code. Set the margins between each text view, button, etc and it will look the same on every phone.

android:layout_marginTop="10dp" 
// change Top to Bottom, Left or Right for what you need.

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