Android 中什么时候应该使用dimens.xml 文件?
例如,在特定布局中,我有以下 XML:
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:columnWidth="48dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth" />
此网格视图特定于此布局,我认为我不会使用具有类似属性的任何其他网格视图。也就是说,代码中的尺寸值特定于该网格视图。
我是否仍然应该将它们移至dimens.xml 文件中,还是就这样保留它们就可以了?如果是这样,仅当该值在多个布局中使用时,我是否应该将值放置在 dimens.xml 文件中?
For instance, in a specific layout I have the following XML:
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:columnWidth="48dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth" />
This grid view is specific to this layout and I don't think I'll be using any other grid views with similar properties. That to say that the dimension values in the code are specific to that grid view.
Should I still move them to a dimens.xml file or it's fine to just leave them like that? If so, should I place values in the dimens.xml file only when that value is used across multiple layouts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将维度值放入
dimens.xml
资源中通常出于三个原因:重用:我需要多个小部件或布局来使用相同的值,而我只想在更新或调整应用程序时更改一次。
密度差异:如果我需要尺寸稍微小于或大于
ldpi -> hdpi
或小 ->大
。从代码中读入:当我在代码中实例化视图并想要应用一些静态尺寸时,请将它们作为
放入
(或dimens.xml
中dpdip
)允许我使用Resources.getDimensionPixelSize()
在 Java 代码中获取缩放值。I drop dimension values into a
dimens.xml
resource typically for three reasons:Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.
Density Difference: If I need the dimension to be slightly smaller or larger from
ldpi -> hdpi
orsmall -> large
.Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in
dimens.xml
asdp
(ordip
) allowing me to get a scaled value in Java code withResources.getDimensionPixelSize()
.补充答案
@Devunwired 列出了使用
dimens.xml
的 3 个理由。以下是如何执行此操作的详细信息。1. 重用
如果您像这样在dimens.xml中设置了一些
dp
或sp
值,您就可以在整个应用程序的多个位置重用它。
然后,当您需要进行更改时,您只需在一处进行即可。
注释
dimen
值。如果您需要更改一组视图而不是另一组视图,那么您将不得不单独返回到每个视图,这违背了目的。2. 尺寸差异
dp
(密度独立像素),这已经解决了密度差异问题除最轻微的情况外的所有情况。因此,在我看来,屏幕尺寸是使用dimens.xml
的一个更重要的因素。8dp
填充在手机上可能看起来很棒,但是当应用程序在平板电脑上运行,看起来太窄了。您可以通过制作两个(或更多)不同版本的dimens.xml
来解决此问题。右键单击您的
res
文件夹,然后选择新建 >值资源文件。然后输入dimens
并选择最小屏幕宽度。输入宽度(7 英寸平板电脑)600。 (还有其他选择尺寸的方法。请参阅文档和此答案了解更多信息。)这将生成另一个
值
将用于最小屏幕宽度为600dp
的设备的文件夹。在 Android 视图中,两个dimens.xml
文件如下所示。现在您可以独立修改它们。
values/dimens.xml
values-sw600dp/dimens.xml
使用
dimen
时,您只需使用在两者中使用的名称来设置它dimens.xml
文件。系统将根据用户使用的设备自动为您选择正确的值。
3. 从代码中读入
有时,以编程方式在
px
和dp
之间进行缩放是一件很痛苦的事情(请参阅 这个答案了解如何)。如果您已经在
dimens.xml
中定义了一个固定的dp
值,如下所示那么您可以轻松地获取它
,并且它已经被转换为用户的任何密度设备的像素有。
Supplemental answer
@Devunwired lists 3 reasons to use
dimens.xml
. Here are the details of how to do that.1. Reuse
If you set some
dp
orsp
value indimens.xml
once like thisyou can reuse it throughout your app in multiple locations.
Then when you need to make a change, you only need to do it in one place.
Notes
dimen
value if they really shouldn't be. If you need to make changes to one set of views but not another, then you will have to go back to each one individually, which defeats the purpose.2. Size Difference
dp
(density independent pixels), this already takes care are the density difference problem for all but the most minor cases. So in my opinion, screen size is a more important factor for usingdimens.xml
.An
8dp
padding might look great on a phone, but when the app is run on a tablet, it looks too narrow. You can solve this problem by making two (or more) different versions ofdimens.xml
.Right click your
res
folder and choose New > Value resource file. Then write indimens
and choose Smallest Screen Width. Write in 600 for the width (7” tablet). (There are other ways of choosing the sizes. See the documentation and this answer for more.)This will make another
values
folder that will be used for devices whose smallest screen width is600dp
. In the Android view the twodimens.xml
files look like this.Now you can modify them independently.
values/dimens.xml
values-sw600dp/dimens.xml
When using your
dimen
you only have to set it with the name you used in bothdimens.xml
files.The system will automatically choose the right value for you depending on the device the user is using.
3. Reading in from code
Sometimes it is a pain scaling programmatically between
px
anddp
(see this answer for how).If you have a fixed
dp
value already defined indimens.xml
like thisThen you can easily get it with
and it will already be converted to pixels for whatever density device the user has.
dimens.xml
文件用于将所有硬编码像素值保存在一处。现在,尽管您现在可能不会重复使用这些值,但将它们放在
dimens.xml
中以供将来参考仍然是一个好主意。此外,遵循标准Android
编程范例可以帮助其他开发人员更快地理解您的代码。这很像strings.xml
,我们在其中放置String
其中一些最终只使用一次! :)The
dimens.xml
file is used to keep all the hard-coded pixel values in one place.Now, although you may not repeatedly use these values right now, it's still a good idea to to place them in
dimens.xml
for future reference. Besides, following a standardAndroid
programming paradigm helps other developers to understand your code faster. This is much like thestrings.xml
where we placeString
s some of which end up being used only once! :)我不知道它是否可以帮助你,但我写了一个小java程序,它允许你使用新的所需值复制维度xml文件,这样你就不再需要逐行手动完成。
https://github.com/Drex-xdev/Dimensions-Scalable-Android
I don’t know if it can help you but I wrote a little java programe that allows you to duplicate a dimension xml file with a new desired value so that you no longer have to do it by hand line by line.
https://github.com/Drex-xdev/Dimensions-Scalable-Android