Android 中什么时候应该使用dimens.xml 文件?

发布于 2024-12-05 20:39:04 字数 548 浏览 6 评论 0原文

例如,在特定布局中,我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里泪两行 2024-12-12 20:39:05

我将维度值放入 dimens.xml 资源中通常出于三个原因:

  1. 重用:我需要多个小部件或布局来使用相同的值,而我只想在更新或调整应用程序时更改一次。

  2. 密度差异:如果我需要尺寸稍微小于或大于ldpi -> hdpi小 ->大

  3. 从代码中读入:当我在代码中实例化视图并想要应用一些静态尺寸时,请将它们作为 放入 dimens.xml 中dp(或 dip)允许我使用 Resources.getDimensionPixelSize() 在 Java 代码中获取缩放值。

I drop dimension values into a dimens.xml resource typically for three reasons:

  1. 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.

  2. Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi or small -> large.

  3. 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 as dp (or dip) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize().

偷得浮生 2024-12-12 20:39:05

补充答案

@Devunwired 列出了使用 dimens.xml 的 3 个理由。以下是如何执行此操作的详细信息。

1. 重用

如果您像这样在dimens.xml中设置了一些dpsp值,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_padding">16dp</dimen>
    <dimen name="large_text_size">30sp</dimen>
</resources>

您就可以在整个应用程序的多个位置重用它。

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

然后,当您需要进行更改时,您只需在一处进行即可。

注释

  • 这与使用样式或主题的效果基本相同。
  • 请注意,如果确实不应该,请不要为两个不同的视图提供相同的 dimen 值。如果您需要更改一组视图而不是另一组视图,那么您将不得不单独返回到每个视图,这违背了目的。

2. 尺寸差异

  • @Devunwired 称之为密度差异,但如果您使用dp(密度独立像素),这已经解决了密度差异问题除最轻微的情况外的所有情况。因此,在我看来,屏幕尺寸是使用 dimens.xml 的一个更重要的因素。

8dp 填充在手机上可能看起来很棒,但是当应用程序在平板电脑上运行,看起来太窄了。您可以通过制作两个(或更多)不同版本的 dimens.xml 来解决此问题。

右键单击您的 res 文件夹,然后选择新建 >值资源文件。然后输入dimens并选择最小屏幕宽度。输入宽度(7 英寸平板电脑)600。 (还有其他选择尺寸的方法。请参阅文档此答案了解更多信息。)

在此处输入图像描述

这将生成另一个将用于最小屏幕宽度为 600dp 的设备的文件夹。在 Android 视图中,两个 dimens.xml 文件如下所示。

输入图片描述这里

现在您可以独立修改它们。

values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">16dp</dimen>
</resources>

values-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">64dp</dimen>
</resources>

使用 dimen 时,您只需使用在两者中使用的名称来设置它dimens.xml 文件。

<LinearLayout
    ...
    android:padding="@dimen/my_default_padding">

</LinearLayout>

系统将根据用户使用的设备自动为您选择正确的值。

3. 从代码中读入

有时,以编程方式在 pxdp 之间进行缩放是一件很痛苦的事情(请参阅 这个答案了解如何)。

如果您已经在dimens.xml中定义了一个固定的dp值,如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_dp_value">16dp</dimen>
</resources>

那么您可以轻松地获取它

int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);

,并且它已经被转换为用户的任何密度设备的像素有。

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 or sp value in dimens.xml once like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_padding">16dp</dimen>
    <dimen name="large_text_size">30sp</dimen>
</resources>

you can reuse it throughout your app in multiple locations.

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

Then when you need to make a change, you only need to do it in one place.

Notes

  • This is basically the same effect as using a style or theme.
  • Be careful not to give two different views the same 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

  • @Devunwired called this Density difference, but if you are using 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 using dimens.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 of dimens.xml.

Right click your res folder and choose New > Value resource file. Then write in dimens 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.)

enter image description here

This will make another values folder that will be used for devices whose smallest screen width is 600dp. In the Android view the two dimens.xml files look like this.

enter image description here

Now you can modify them independently.

values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">16dp</dimen>
</resources>

values-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">64dp</dimen>
</resources>

When using your dimen you only have to set it with the name you used in both dimens.xml files.

<LinearLayout
    ...
    android:padding="@dimen/my_default_padding">

</LinearLayout>

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 and dp (see this answer for how).

If you have a fixed dp value already defined in dimens.xml like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_dp_value">16dp</dimen>
</resources>

Then you can easily get it with

int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);

and it will already be converted to pixels for whatever density device the user has.

玩套路吗 2024-12-12 20:39:05

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 standard Android programming paradigm helps other developers to understand your code faster. This is much like the strings.xml where we place Strings some of which end up being used only once! :)

≈。彩虹 2024-12-12 20:39:05

我不知道它是否可以帮助你,但我写了一个小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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文