如何在 Android 中使用 dimens.xml? [英] How to use dimens.xml in Android?

查看:14
本文介绍了如何在 Android 中使用 dimens.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我设计布局时,由于可维护性主题,我将所有维度集中在 dimens.xml 中.我的问题是这是否正确.最好的做法是什么?关于这方面的信息很少,什么都没有.我知道将布局的所有字符串集中在strings.xml 上,颜色集中在colors.xml 上是个好主意.但关于尺寸?

When I design a layout, I centralize all dimensions in dimens.xml because of topics of maintainability. My question is if this is correct or not. What would it be the best good practice? There is very little information about this, nothing. I know it's good idea to centralize all strings of a layout on strings.xml, colors on colors.xml. But about dimensions?

例如:

<TableLayout
    android:id="@+id/history_detail_rows_submitted"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cebroker_history_detail_rows_border"
    android:collapseColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/history_detail_rows_margin_vertical"
        android:background="@color/cebroker_history_detail_rows_background"
        android:gravity="center"
        android:paddingBottom="@dimen/history_detail_rows_padding_vertical"
        android:paddingLeft="@dimen/history_detail_rows_padding_horizontal"
        android:paddingRight="@dimen/history_detail_rows_padding_horizontal"
        android:paddingTop="@dimen/history_detail_rows_padding_vertical">

        <TextView
            android:layout_width="match_parent"
            android:drawableLeft="@mipmap/ic_history_detail_submitted_by"
            android:drawablePadding="@dimen/history_detail_rows_textviews_padding_drawable"
            android:gravity="left|center"
            android:paddingRight="@dimen/history_detail_rows_textviews_padding"
            android:text="@string/history_detail_textview_submitted_by"
            android:textColor="@color/cebroker_history_detail_rows_textviews"
            android:textSize="@dimen/history_detail_rows_textviews_text_size" />

推荐答案

如何使用dimens.xml

  1. 通过右键单击 values 文件夹并选择 New > 创建一个新的 dimens.xml 文件.值资源文件.为名称写入 dimens.(您也可以将其称为 dimendimensions.名称并不重要,只有它将包含的 dimen 资源类型.)

  1. Create a new dimens.xml file by right clicking the values folder and choosing New > Values resource file. Write dimens for the name. (You could also call it dimen or dimensions. The name doesn't really matter, only the dimen resource type that it will include.)

添加dimen 名称和值.

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

值可以是 dppxsp.

使用xml中的值

 <TextView
     android:padding="@dimen/my_value"
     ... />

或在代码中

 float sizeInPixels = getResources().getDimension(R.dimen.my_value);

何时使用dimens.xml

感谢这个答案提供更多想法.

When to use dimens.xml

Thanks to this answer for more ideas.

  1. 重用值 - 如果您需要在整个应用中的多个位置使用相同的维度(例如,Activity 布局填充或 TextView textSize),则使用单个 dimen 值将使以后更容易调整.这与使用样式和主题的想法相同.

  1. Reusing values - If you need to use the same dimension multiple places throughout your app (for example, Activity layout padding or a TextView textSize), then using a single dimen value will make it much easier to adjust later. This is the same idea as using styles and themes.

支持多屏幕 - 8dp 的填充在手机上可能看起来不错,但在 10 英寸屏幕上可能很糟糕药片.您可以创建多个 dimens.xml 以用于不同的屏幕.这样你就可以为手机设置8dp,为平板电脑设置64dp.要创建另一个 dimens.xml 文件,请右键单击您的 res 文件夹并选择 New >值资源文件.(有关详细信息,请参阅此答案)

Supporting Multiple Screens - A padding of 8dp might look fine on a phone but terrible on a 10" tablet. You can create multiple dimens.xml to be used with different screens. That way you could do something like set 8dp for the phone and 64dp for the tablet. To create another dimens.xml file, right click your res folder and choose New > Value resource file. (see this answer for details)

方便的 dppx 代码转换 - 在代码中,您通常需要处理像素值.但是,您仍然需要考虑设备密度,并且以编程方式进行的转换很烦人.如果你有一个恒定的 dp 值,你可以像这样为 float 轻松地以像素为单位获得它:

Convenient dp to px code conversion - In code you usually need to work with pixel values. However you still have to think about the device density and the conversion is annoying to do programmatically. If you have a constant dp value, you can get it in pixels easy like this for float:

 float sizeInPixels = getResources().getDimension(R.dimen.my_value);

或者对于 int :

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

我在我更全面的回答中详细说明了如何做这些事情.

I give many more details of how to do these things in my fuller answer.

  • 不要将您的值放在 dimens.xml 中,否则会使它们更难以维护.一般来说,只要它不属于我上面列出的类别.使用 dimens.xml 会使代码更难阅读,因为您必须在两个文件之间来回切换才能查看实际值.个人观点不值得(在我看来).

  • Don't put your values in dimens.xml if it is going to make them more difficult to maintain. Generally that will be whenever it doesn't fall into the categories I listed above. Using dimens.xml makes the code harder to read because you have to flip back and forth between two files to see what the actual values are. It's not worth it (in my opinion) for individual Views.

字符串是不同的.所有字符串都应该放在像 strings.xml 这样的资源文件中,因为在国际化应用程序时几乎所有的字符串都需要翻译.另一方面,大多数维度值不需要针对不同的位置进行更改.Android Studio 似乎支持这种推理.直接在布局 xml 中定义字符串会给出警告,但定义 dp 值不会.

Strings are different. All strings should go in a resource file like strings.xml because almost all strings need to be translated when internationalizing your app. Most dimension values, on the other hand, do not need to change for a different locality. Android Studio seems to support this reasoning. Defining a string directly in the layout xml will give a warning but defining a dp value won't.

这篇关于如何在 Android 中使用 dimens.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆