简体   繁体   中英

Make RelativeLayout to fit all screen size

I'm trying to modify my Relative Layout to fit all the screen size. I understood that my problem was that I was manually setting the margin for every single element on the screen, because it was easier to set on the editor in Android Studio, but obviously, as soon as I tried the app on another device the elements were in a mess. Now, I modified it by using the "match_parent" and wrap_content commands to make it more flexibly, and this is the result when I try it on a Pixel C: 在此处输入图片说明

The problem is when I change device, because for example if I try It on a Galaxy Tab this is the result: 在此处输入图片说明

This is the code:

    <?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:id="@+id/slidelinearlayout"
    android:background="@drawable/background_main2">

<LinearLayout
    android:id="@+id/upperLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="140dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/slideimg"
            android:layout_width="400dp"
            android:layout_height="350dp"
            android:layout_marginRight="150dp" />
        <TextView
            android:id="@+id/txttitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="20dp"
            android:text="Senza nome"
            android:textColor="@android:color/black"
            android:textSize="40sp"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/upperTableHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="160dp"
            android:text="Table Header here"
            android:textColor="@android:color/holo_red_light"
            android:textSize="30sp" />

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/tl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/t2"
            android:layout_alignParentEnd="true" >

        <TableRow
            android:id="@+id/tr_item_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Flessibilità"
                android:textColor="#030101"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/f_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_2"
                android:layout_width="202dp"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Fluidità"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/fl_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Originalità"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/o_item_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Elaborazione"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/el_item_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Titolo"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/t_item_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>
    </TableLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/upperLayout"
    android:layout_alignParentRight="true"
    android:layout_marginTop="30dp"
    android:layout_marginRight="80dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lowerTableHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Table Header here"
        android:textColor="@android:color/holo_red_light"
        android:textSize="30sp" />

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/tl_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/tl">

        <TableRow
            android:id="@+id/tr_item_6"
            android:layout_width="222dp"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/t1_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Tempo di reazione"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/tempo_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/t2_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Tempo di completamento"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/tempo_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/n_cancellature"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Numero cancellature"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/n_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>
    </TableLayout>
</LinearLayout>

You can use the SDP to set the size of controls or text for different devices.

在此处输入图片说明

An android SDK that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.

Link - https://github.com/intuit/sdp

you can create Different layout directory in res

and Design ui exclusively for that screen size

Android automatically detects which size must to use

like : layout (this is default and its for all devices) layout-sw600 (for 7 and 8 inch screens) layout-sw720 (for 10 ,11 and 12 screens)

for more info watch this

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