简体   繁体   中英

Android layout looks different on different devices

I am developing an Android app and now I write a layout and it looks different on different devices.

First here is how it looks like:

1 (correct one) 在此处输入图像描述

2 (incorrect one) 在此处输入图像描述

As we can see on first upper part (top until horizontal line) has correct weight = 2 and lower part weight = 1. On the second picture those proportions seem to be totally wrong. Why?

Here is my layout:

<LinearLayout 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:orientation="vertical"
android:layout_marginTop="?actionBarSize"
android:weightSum="3">

<RelativeLayout
    android:id="@+id/rl5"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:layout_marginBottom="5dp"
    android:orientation="vertical"
    android:paddingHorizontal="10dp"
    >

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/LL2"

        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_benchmarks"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#ffffff"
            android:dividerHeight="1dp"
            android:footerDividersEnabled="true"
            android:scrollbars="vertical"
            android:headerDividersEnabled="true" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b_add_benchmark"
        android:baselineAligned="false">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/input_layout_email"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/ben_hint_name"
                android:inputType="text"
                android:textSize="20sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/input_layout_email2"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/ben_hint_size"
                android:inputType="number"
                android:textSize="20sp" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/b_add_benchmark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_selector"
        android:padding="10dp"
        android:text="@string/add_ben"
        android:textSize="20sp"
        tools:gravity="center" />
</RelativeLayout>






<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginBottom="?attr/actionBarSize">

    <View
        android:id="@+id/View1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="@dimen/margin_top_of_bar"
        android:layout_marginBottom="5dp"
        android:background="@android:color/darker_gray" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/View1">

        <ImageView
            android:id="@+id/iv7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.2"
            android:src="@drawable/ic_ben_measure"
            android:scaleType="fitXY"
            android:contentDescription="@string/benchmark_background" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_gravity="top|center"
                android:layout_marginEnd="20dp"
                android:background="@drawable/textview_bg_from_left"
                android:padding="5dp"
                android:text="@string/set_benchmark"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#FFF"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/textView19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textview"
                android:layout_centerHorizontal="true"
                android:paddingTop="10dp"
                android:text="@string/benchmark"
                android:textSize="19sp" />

            <TextView
                android:id="@+id/tv_benchmark_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView19"
                android:layout_centerHorizontal="true"
                android:text="." tools:ignore="HardcodedText"
                android:textSize="19sp" />
        </RelativeLayout>
    </FrameLayout>
</RelativeLayout>

for that you have to use scrollview or Proper linearLayout weightsum(Copy and check below code).

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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_marginTop="?actionBarSize"
    android:orientation="vertical"
    android:weightSum="2">

    <RelativeLayout
        android:id="@+id/rl5"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1.7"
        android:orientation="vertical"
        android:paddingHorizontal="10dp">

        <LinearLayout
            android:id="@+id/LL1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/LL2"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_benchmarks"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="#ffffff"
                android:dividerHeight="1dp"
                android:footerDividersEnabled="true"
                android:headerDividersEnabled="true"
                android:scrollbars="vertical" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LL2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/b_add_benchmark"
            android:baselineAligned="false">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_email"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="benHint"
                    android:inputType="text"
                    android:textSize="20sp" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_email2"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <EditText
                    android:id="@+id/editText2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="hint size"
                    android:inputType="number"
                    android:textSize="20sp" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/b_add_benchmark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:background="@drawable/button_green"
            android:padding="10dp"
            android:text="Add"
            android:textSize="20sp"
            tools:gravity="center" />
    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="?attr/actionBarSize"
        android:layout_weight="0.25">

        <View
            android:id="@+id/View1"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:background="@android:color/darker_gray" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/View1">

            <ImageView
                android:id="@+id/iv7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.2"
                android:contentDescription="Backgrounf"
                android:scaleType="fitXY"
                android:src="@drawable/logo" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/textview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_gravity="top|center"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/button_green"
                    android:padding="5dp"
                    android:text="Set"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#FFF"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/textView19"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textview"
                    android:layout_centerHorizontal="true"
                    android:paddingTop="10dp"
                    android:text="benchmark"
                    android:textSize="19sp" />

                <TextView
                    android:id="@+id/tv_benchmark_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textView19"
                    android:layout_centerHorizontal="true"
                    android:text="."
                    android:textSize="19sp"
                    tools:ignore="HardcodedText" />
            </RelativeLayout>
        </FrameLayout>
    </RelativeLayout>
</LinearLayout>

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