如何通过不同的屏幕尺寸保持与XML drawable的一致性?

如何通过不同的屏幕尺寸保持与XML drawable的一致性?,第1张

概述我正在尝试为计算器应用程序创建自己的按钮.我的麻烦是,我发现很难支持所有不同的屏幕尺寸.基本上我有很多按钮,像这样创建<Buttonandroid:text="Button"android:id="@+id/button1"android:background="@drawableest"android:layout_height="60dip"android:layo

我正在尝试为计算器应用程序创建自己的按钮.

我的麻烦是,我发现很难支持所有不同的屏幕尺寸.

基本上我有很多按钮,像这样创建

<button androID:text="button" androID:ID="@+ID/button1"    androID:background="@drawable/test"    androID:layout_height="60dip" androID:layout_wIDth="60dip" androID:layout_margin="5dip"></button>

我的背景是XML drawable

<?xml version="1.0" enCoding="utf-8"?><@R_809_3419@ xmlns:androID="http://schemas.androID.com/apk/res/androID"><!-- Bottom 2dp Shadow --><item>    <shape androID:shape="rectangle">        <gradIEnt androID:startcolor="#E0E0E0"         androID:endcolor="#373737"            androID:angle="315" />        <corners androID:radius="20dip" />    </shape></item><!-- White top color --><item androID:top="5dip" androID:left="5dip" androID:right="5dip"    androID:bottom="5dip">    <shape androID:shape="rectangle">        <gradIEnt androID:startcolor="#9E9E9E" androID:endcolor="#C5C5C5"            androID:angle="270" />        <corners androID:radius="20dip" />    </shape></item>

基本上是一个圆角的背景和看起来像斜面的东西,以及一些使它看起来很漂亮的渐变!

3.2 HVGA上的按钮尺寸正确

但是当我查看3.7 FWVGA中的按钮时,宽高比会丢失,并且尺寸与3.2相同,但由于在此屏幕上有更多像素可供使用,因此图像尺寸不正确.

反正在这种情况下是否保持一致性???

解决方法:

问题

正如您已经提到的,问题是可用的屏幕空间更多.这意味着您必须根据填充可用空间来调整按钮的大小,例如用4个按钮填充显示器的可用宽度,并缩放它们的高度,使图形的纵横比保持不变.

你在这里使用dp单位,这在定义布局时绝对是好的做法.但在这种情况下,它告诉布局“这个东西有一个固定大小的XX dp”(您可以将dp视为固定单位,固定数量只是由显示布局的设备决定).你不想要一个固定的大小.您希望按钮以动态方式拉伸.

一个办法

为上面的布局提供解决方案:您可以使用linearLayout及其layout_weight属性.这里有一些简短(不完整)的示例代码来说明原理:

<linearLayout androID:orIEntation="horizontal"              androID:layout_wIDth="fill_parent"              androID:layout_height="wrap_content">    <button androID:layout_wIDth="0dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            />    <button androID:layout_wIDth="0dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            />    <button androID:layout_wIDth="0dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            />    <button androID:layout_wIDth="0dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            /></linearLayout>

这里发生了什么?

首先,我们创建一个外部linearLayout,它填充整个父宽度(=整个屏幕宽度),并根据内容调整它的高度.标准的东西.

然后我们添加4个按钮.这里的相关部分是layout_weight.这告诉每个按钮“尽可能地填充可用空间”.由于现在每个人都试图伸展,我们必须定义每个按钮获得多少空间.这是给layout_weight的数字.在这里,每个按钮的数字相同.这意味着每个按钮的尺寸与其他任何尺寸相同.那么布局如何确定多少空间呢?它计算它的子项的总重量,这里是4,并根据child_weight / total_weight的比率给每个子项空间.所以每个按钮占宽度的1/4. (你也可以指定任何其他数字,如果给每个按钮增加重量3,每个按钮将获得3/12的宽度,根据数学仍然是1/4 :))

还有一个问题:为什么layout_wIDth =“0dp”?据我所知,这只是阻止了一些不必要的内部计算.您必须知道的是,在使用layout_weight属性时,使用0dp作为缩放维度.如果你想了解更多,你可以研究一下.

样品

以下是实践中的两个截图:

2.7“QVGA

3.7“FWVGA

总结

以上是内存溢出为你收集整理的如何通过不同的屏幕尺寸保持与XML drawable的一致性?全部内容,希望文章能够帮你解决如何通过不同的屏幕尺寸保持与XML drawable的一致性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1114108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存