簡體   English   中英

Android布局在設備之間不一致,而不是使用dp

[英]Android layout not consistent across devices instead of using dp

第一張圖片的 屏幕截圖其他手機的屏幕截圖我有一個LinearLayout,RelativeLayout,LinearLayout和一個ImageView。這基本上是我的應用程序的登錄頁面,盡管使用dp時我的輸出在所有設備上都不相同。 有兩個不同手機的屏幕截圖。 提前致謝

<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>

您需要為主要的屏幕密度存儲區和屏幕尺寸創建不同的布局文件。 這樣,您將可以微調布局以在多個顯示器上看起來一致。

您可以從預覽窗口輕松地為特定存儲桶創建新的布局:

  • 點擊這個圖標 在此處輸入圖片說明 在左上角。
  • 然后選擇Create Other...
  • 將出現一個彈出窗口,然后從“ Available Qualifiers ”中為不同密度桶的不同屏幕尺寸“ Density選擇size項目。
  • 選擇所需的項目后,請按>>鍵,然后可以為要創建新布局文件的女巫選擇合適的存儲桶。
  • 之后,按OK,Android Studio將為您創建當前布局的副本並將其放置在適合您的文件夾中,您所需要做的就是對其進行編輯,以使其看起來像您希望的樣子具體的布局。

您應該像這樣使用ScrollView:

<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>

為了與所有設備更兼容,請嘗試使用“ wrap_content”或“ match_parent”作為高度和寬度。 如果圖像使您需要在代碼中聲明dp,則請通過以下鏈接: https : //developer.android.com/guide/practices/screens_support.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM