Android layout not consistent across devices inste

2019-09-19 19:10发布

问题:

Screenshot of first image Screenshot of other phoneI have a LinearLayout ,RelativeLayout,LinearLayout and a ImageView in it.This is basically a landing page of my app,inspite of using the dp my output is not same on the all device. There is the screenshot of two different phones. Thanks in advance

<LinearLayout
        android:id="@+id/lb_LinearLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
        android:id="@+id/lb_Relative_HomeScreen"
        android:layout_width="match_parent"
        android:layout_height="450dp">
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="350dp">
           ``   <ImageView
                android:id="@+id/lb_Background_Image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY" />
            </LinearLayout>
            <TextView
                android:id="@+id/lb_Welcome"
                android:text="@string/welcome"
                android:gravity="start"
                style="@style/Heading2.yellow"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_gravity="start|center_vertical"
                android:layout_above="@+id/lb_Descrption"
                android:layout_marginLeft="20dp"
                />
...
</RelativeLayout>
</LinearLayout>

回答1:

You need to create different layout files for the major screen density buckets and screen sizes. This way you will be able to fine tune your layout to look consistent across multiple displays.

You can easily create a new layout for a specific bucket from the preview window:

  • Click on this icon in the left corner.
  • Then select Create Other....
  • A popup window will appear then select from the Available Qualifiers the size item for the different screen sizes of Density for the different density buckets.
  • After you selected the desired item press the >> key, and you can select the appropriate bucket for witch you wish to create a new layout file.
  • After this you press ok and the android studio creates for you a copy of the current layout and places it in the appropriate folder for you, all that you need to do is to edit it so that it look the way you want it to look on the specific layout.


回答2:

You should use ScrollView like this :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:id="@+id/lb_LinearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:id="@+id/lb_Relative_HomeScreen"
        android:layout_width="match_parent"
        android:layout_height="450dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="350dp">
            ``   <ImageView
            android:id="@+id/lb_Background_Image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY" />
        </LinearLayout>
        <TextView
            android:id="@+id/lb_Welcome"
            android:text="@string/welcome"
            android:gravity="start"
            style="@style/Heading2.yellow"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="start|center_vertical"
            android:layout_above="@+id/lb_Descrption"
            android:layout_marginLeft="20dp"
            />
        ...
    </RelativeLayout>
</LinearLayout>

And to be more compatible with all the devices, try to use "wrap_content" or "match_parent" for height and width. If images are making you to declare dp in your code, then please go through this link : https://developer.android.com/guide/practices/screens_support.html