应该在什么时候dimens.xml文件在Android中使用? [英] When should the dimens.xml file be used in Android?

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

问题描述

有关例如,在一个特定的布局我有下面的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" />

这网格视图是特定于这种布局,我将使用具有类似性质的任何其它网格视图我不认为。这就是说,在code尺寸值特定于该网格视图。

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.

我应该还是将它们移动到dimens.xml文件或它的罚款,只是让他们这样?如果是这样,我应该把值在dimens.xml文件,只有当该值在多个布局中使用?

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?

推荐答案

我把尺寸值成 dimens.xml 资源通常有三个原因:

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


  1. 再利用:我需要多个部件或布局,使用相同的价值,我只希望更改一次更新或整个应用程序调整时

  2. 密度差:如果我需要的尺寸要略小或LDPI更大 - >华电国际或小 - >大

  3. 从code阅读在:当我在实例化code美景,要应用一些静态的尺寸,将它们放在 dimens.xml 作为DP或下降可以让我在Java中code进行换算值与 Resources.getDimensionPixelSize()

  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 code and want to apply some static dimensions, putting them in dimens.xml as "dp" or "dip" allows me to get a scaled value in Java code with Resources.getDimensionPixelSize()

心连心!

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

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