android 默认dimens_如何在Android中使用dimens.xml?

本文探讨了在Android应用开发中,将所有尺寸集中到dimens.xml文件中的做法是否正确,并提供了最佳实践。通过使用dimens.xml,可以提高代码的可维护性,方便调整布局,并支持不同屏幕尺寸。此外,它还方便了dp到px的转换,简化了多屏幕适配。然而,不建议将所有值都放入dimens.xml,以免降低代码可读性。对于不需要根据地区变化的尺寸,使用dimens.xml是有益的,而字符串资源则应始终放在strings.xml中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

For example:

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="*">

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">

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" />

解决方案

How to use dimens.xml

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

Add a dimen name and value.

16dp

Values can be in dp, px, or sp.

Use the value in xml

android:padding="@dimen/my_value"

... />

or in code

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

When to use dimens.xml

Thanks to this answer for more ideas.

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.

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)

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);

or this for int :

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

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

When not to use 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 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值