[英]Android: Possible to use DP on a layout and make it look the same across devices?
[英]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>
您需要為主要的屏幕密度存儲區和屏幕尺寸創建不同的布局文件。 這樣,您將可以微調布局以在多個顯示器上看起來一致。
您可以從預覽窗口輕松地為特定存儲桶創建新的布局:
您應該像這樣使用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.