如何在 android 中创建应支持所有设备的布局?
How to create a layout in android which should support all the devices?
这个问题可能被问了 1000 多次,我仍然很困惑,哪一个才是正确的方法?当我用谷歌搜索时,我找到了一些相关链接 link1 ,link2,link3 etc.Solution 我发现的是在 xml design.All 中使用相对布局布局将在布局文件夹中(如果您正在创建 phone 应用程序)并且尺寸应在 dimens.xml 中给出(在值中-mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi 文件夹)以支持所有屏幕。
我如何为图像提供 marginTop view.I 正在计算 this:in 比率
(1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10dp-15dp-20dp-30dp-40dp)。
我的做法是否正确?
我在 drawable-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi) 文件夹中给出了不同分辨率的不同图像。
我的问题在values-mdpi/dimens.xml-margintop-10dp,
values-hdpi/dimens.xml-margintop-15dp,values-xhdpi/dimens.xml-margintop-20dp etc.Giving 不同屏幕的尺寸值比例为 (1 : 1.5 : 2 : 3 : 4) 是正确的吗?
在 Main.xml:
<RelativeLayout
android:id="@+id/rl_submain"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_papa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/s_mtop"
android:background="@drawable/papa1"
android:gravity="center"/>
<TextView
android:id="@+id/tv_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="@dimen/s_mbot"
android:textSize="12sp"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:text="@string/poweredby"/>
</RelativeLayout>
<resources>
<!--Splash-->mdpi
<dimen name="s_mtop">10dp</dimen>
<dimen name="s_mbot">2dp</dimen>
<resources>
<!--Splash-->hdpi
<dimen name="s_mtop">15dp</dimen>
<dimen name="s_mbot">3dp</dimen>
<resources>
<!--Splash-->xhdpi
<dimen name="s_mtop">20dp</dimen>
<dimen name="s_mbot">4dp</dimen>
<resources>
<!--Splash-->xxhdpi
<dimen name="s_mtop">30dp</dimen>
<dimen name="s_mbot">6dp</dimen>
<resources>
<!--Splash-->xxxhdpi
<dimen name="s_mtop">40dp</dimen>
<dimen name="s_mbot">8dp</dimen>
how can i give marginTop for an image view.I am doing calculation like
this:in ratios
(1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10dp-15dp-20dp-30dp-40dp).
Whether my approach is correct or?
尺寸比 (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi) 是正确的,但不适用于尺寸单位 dp/dip.. .它用于 px
dp :Density-independent Pixels - An abstract unit that is based on the
physical density of the screen. These units are relative to a 160 dpi
(dots per inch) screen, on which 1dp is roughly equal to 1px. When
running on a higher density screen, the number of pixels used to draw
1dp is scaled up by a factor appropriate for the screen's dpi.
Likewise, when on a lower density screen, the number of pixels used
for 1dp is scaled down.
有关different dimension unit refer doc
的更多详细信息
因此,根据您的示例,让 marginTop = 10dp 然后根据比率 (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10px- 15px-20px-30px-40px)
than 意味着你也可以做 marginTop =10px 但它会保持 10px 所有..所以更高的 dpi 在这种情况下 marginTop 看起来很小..
为了dip/dp超过px你can refer here
sp类似于dp;但它在字体大小的上下文中使用,原因是参考尺寸单位文档
You can also refer the converter here
dimensions should be given in dimens.xml(which is in
values-mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi folders) to support all screen.
再次谈到维度,你不应该使用 values-mdpi、hdpi...等等 on.Because 如果你使用单位 dp,那么它与提到的屏幕密度无关。
如果您使用单位 px,那么您可以使用 values-mdpi、values-hpdi...等等,但这同样会成为手动计算的开销,并可能通过获取不需要的值而引发错误。
所以维度值最好只根据屏幕尺寸而不是屏幕密度来延迟。因此您的布局或值文件夹可以按照 Android OS(API) 版本,屏幕尺寸如正常、大或如 link1 中所述,link2 并在 Eenvicible 的评论中
根据评论更新
如果你通过 Supporting Different Screens then you will realize that
for layout we have folder structure layout-normal, layout-large, layout-large-land...but again this might vary
In the same link you can see drawable-mdpi..and so on. Here images we use are generally not density independent so we need same image in different densities and we add it in accordingly in folder
for values we have folder structure like values, values-fr in case you Supporting Different Language
关于values-mdpi,values-hpdi我上面已经解释过了
请仔细阅读每一个link..bcoz我指的是官方文档
为什么不使用权重来代替布局。在图像上方有一个空白视图并根据您的要求对其进行加权,它会自动拉伸。您可以找到示例 here
或者这样试试。根据您对空白布局
的需要,为 space 赋予权重
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:layout_weight="0.2"
android:id="@+id/view1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_register"
android:layout_weight="0.8"
/>
</LinearLayout>
</RelativeLayout>
是的,您的 dimens.xml 文件中的值完全错误。请删除你所有的不同dimens.xml。当您使用图像编辑器编辑它们时,不同的密度适用于实际图像。
如果您希望图像周围有边距,请使用带有 weights or relative 布局的线性布局,但不要使用 dps。加权线性布局或相对布局更加灵活。
但是如果你选择 dps,我也不推荐,知道你的 dimens.xml 中的一组测量值就足够了。
理想情况下,不要创建超过您需要的 xml layout/dimens 变体。如果需要,您可以调整它们,但在开始添加更多限定符之前,请确保您正在解决实际问题。
这个问题可能被问了 1000 多次,我仍然很困惑,哪一个才是正确的方法?当我用谷歌搜索时,我找到了一些相关链接 link1 ,link2,link3 etc.Solution 我发现的是在 xml design.All 中使用相对布局布局将在布局文件夹中(如果您正在创建 phone 应用程序)并且尺寸应在 dimens.xml 中给出(在值中-mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi 文件夹)以支持所有屏幕。
我如何为图像提供 marginTop view.I 正在计算 this:in 比率 (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10dp-15dp-20dp-30dp-40dp)。 我的做法是否正确?
我在 drawable-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi) 文件夹中给出了不同分辨率的不同图像。
我的问题在values-mdpi/dimens.xml-margintop-10dp, values-hdpi/dimens.xml-margintop-15dp,values-xhdpi/dimens.xml-margintop-20dp etc.Giving 不同屏幕的尺寸值比例为 (1 : 1.5 : 2 : 3 : 4) 是正确的吗? 在 Main.xml:
<RelativeLayout
android:id="@+id/rl_submain"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_papa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/s_mtop"
android:background="@drawable/papa1"
android:gravity="center"/>
<TextView
android:id="@+id/tv_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="@dimen/s_mbot"
android:textSize="12sp"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:text="@string/poweredby"/>
</RelativeLayout>
<resources>
<!--Splash-->mdpi
<dimen name="s_mtop">10dp</dimen>
<dimen name="s_mbot">2dp</dimen>
<resources>
<!--Splash-->hdpi
<dimen name="s_mtop">15dp</dimen>
<dimen name="s_mbot">3dp</dimen>
<resources>
<!--Splash-->xhdpi
<dimen name="s_mtop">20dp</dimen>
<dimen name="s_mbot">4dp</dimen>
<resources>
<!--Splash-->xxhdpi
<dimen name="s_mtop">30dp</dimen>
<dimen name="s_mbot">6dp</dimen>
<resources>
<!--Splash-->xxxhdpi
<dimen name="s_mtop">40dp</dimen>
<dimen name="s_mbot">8dp</dimen>
how can i give marginTop for an image view.I am doing calculation like this:in ratios (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10dp-15dp-20dp-30dp-40dp). Whether my approach is correct or?
尺寸比 (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi) 是正确的,但不适用于尺寸单位 dp/dip.. .它用于 px
dp :Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down.
有关different dimension unit refer doc
的更多详细信息因此,根据您的示例,让 marginTop = 10dp 然后根据比率 (1:1.5:2:3:4)-(mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi)-(10px- 15px-20px-30px-40px)
than 意味着你也可以做 marginTop =10px 但它会保持 10px 所有..所以更高的 dpi 在这种情况下 marginTop 看起来很小..
为了dip/dp超过px你can refer here
sp类似于dp;但它在字体大小的上下文中使用,原因是参考尺寸单位文档
You can also refer the converter here
dimensions should be given in dimens.xml(which is in values-mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi folders) to support all screen.
再次谈到维度,你不应该使用 values-mdpi、hdpi...等等 on.Because 如果你使用单位 dp,那么它与提到的屏幕密度无关。
如果您使用单位 px,那么您可以使用 values-mdpi、values-hpdi...等等,但这同样会成为手动计算的开销,并可能通过获取不需要的值而引发错误。
所以维度值最好只根据屏幕尺寸而不是屏幕密度来延迟。因此您的布局或值文件夹可以按照 Android OS(API) 版本,屏幕尺寸如正常、大或如 link1 中所述,link2 并在 Eenvicible 的评论中
根据评论更新
如果你通过 Supporting Different Screens then you will realize that
for layout we have folder structure layout-normal, layout-large, layout-large-land...but again this might vary
In the same link you can see drawable-mdpi..and so on. Here images we use are generally not density independent so we need same image in different densities and we add it in accordingly in folder
for values we have folder structure like values, values-fr in case you Supporting Different Language
关于values-mdpi,values-hpdi我上面已经解释过了
请仔细阅读每一个link..bcoz我指的是官方文档
为什么不使用权重来代替布局。在图像上方有一个空白视图并根据您的要求对其进行加权,它会自动拉伸。您可以找到示例 here
或者这样试试。根据您对空白布局
的需要,为 space 赋予权重 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:layout_weight="0.2"
android:id="@+id/view1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_register"
android:layout_weight="0.8"
/>
</LinearLayout>
</RelativeLayout>
是的,您的 dimens.xml 文件中的值完全错误。请删除你所有的不同dimens.xml。当您使用图像编辑器编辑它们时,不同的密度适用于实际图像。
如果您希望图像周围有边距,请使用带有 weights or relative 布局的线性布局,但不要使用 dps。加权线性布局或相对布局更加灵活。
但是如果你选择 dps,我也不推荐,知道你的 dimens.xml 中的一组测量值就足够了。
理想情况下,不要创建超过您需要的 xml layout/dimens 变体。如果需要,您可以调整它们,但在开始添加更多限定符之前,请确保您正在解决实际问题。