繁体   English   中英

不同屏幕尺寸的android布局问题

[英]issue with android layout for different screen sizes

我有一个带有各种按钮和文本视图的android应用。 使用dp的测量,按钮,文本视图和edittext字段在屏幕上间隔开。 因此,例如,一个按钮将具有:“从左侧10dp开始的margintop”

问题在于高密度电话。 我为更高密度的屏幕创建了一个新的布局,并将其命名为layout-large或layout-sw600-sw720(因为问题出在银河s3上)。

但是电话仍然会继续调用适合于480 x 800密度屏幕的常规布局文件。

我读到s3称为道德布局文件。 因此,如果我将普通文件中的xml更改为高密度的xml,那么如何降低密度布局xml? 我应该叫哪个文件名,电话会再次调用该文件还是普通文件?

XML布局摘录:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/duroodscreen"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >

<Button
android:id="@+id/dmute"
android:layout_width="48dp"
android:layout_height="33dp"
android:layout_alignParentLeft="true"
android:background="@drawable/soundon" />

<Button
android:id="@+id/dreset"
style="?android:attr/buttonStyleSmall"
android:layout_width="48dp"
android:layout_height="33dp" 
android:layout_alignParentRight="true"
android:background="@drawable/customresetbutton" />

<TextView
android:id="@+id/dcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/dmute"
android:layout_marginLeft="25dp"
android:layout_marginTop="36dp"
android:gravity="center"
android:singleLine="true"
android:text="Numbers"
android:textSize="25sp" />

清单中的引用:

<supports-screens 
android:resizeable="true"
android:smallScreens="true" 
android:normalScreens="true" 
android:largeScreens="true"
android:anyDensity="true" />

在这种情况下,很痛苦的是,最好仅为Galaxy S3进行布局,而不是尝试重写所有内容以纠正一个制造商的错误。

因此,您将:

获取手机型号

if "galaxy S3" 
   layout with "my_layout_galaxy_s3"
else -> layout with 
   layout with "my_layout"

不要将屏幕尺寸与密度混淆。 手机屏幕大小正常。 所以您看到的没问题。 还要注意, sw600限定词是指600 dp的宽度,而不是像素。

要为其他密度提供资源,请使用密度限定符(mdpi,hdpi等)。

垂直布局表示屏幕的尺寸 ,而不是密度

您必须创建layout-normal-mdpilayout-normal-hdpi文件

layout-normal-xhdpi S3使用layout-normal-xhdpi

为什么在各处都使用这么多像素值? 像素值只能用于较小的事物,例如在元素之间填充一些像素。 这样做的原因是首先要避免此类问题。

例如,为什么要指定按钮的宽度和高度? 首先,他们没有文本,所以为什么不使用ImageButton? 其次,只需将图像设置为所需大小,然后使用wrap_content。 与文本字段相同。 android布局的想法实际上是避免进行所有像素计算,而是相对地进行所有操作,以便设备可以根据硬件做出自己的决定,以使其外观最佳。

如果您真的想与系统抗争,那么一路做起来可能会更容易-使用AbsoluteLayout并为所有内容指定精确的像素坐标。 否则,请拥抱Android的工作方式或为痛苦的世界做好准备。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM