Android layout not consistent across devices instead of using dp

假装没事ソ 提交于 2019-12-25 07:41: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



来源:https://stackoverflow.com/questions/36431907/android-layout-not-consistent-across-devices-instead-of-using-dp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!