如何在Android中使用dimens.xml?

How to use dimens.xml in Android?

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

例如:

<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" />
But about dimensions?

根据官方 Android 文档 "A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one <resources> element"

更多详情请参考http://developer.android.com/guide/topics/resources/more-resources.html

在这个 post 中,Devunwired 给出了为什么使用 dimens.xml When should the dimens.xml file be used in Android?

的三个重要原因

添加一个 xml 文件 dimens.xml 这用于支持多个设备。

 <resources>
 <!-- Default screen margins, per the Android Design guidelines. -->
  <dimen name="iconarrow">1dp</dimen>
  <item name="text_view_padding" type="integer">100</item>
</resources>

然后你可以像这样在你的代码中使用它 java 代码

textview.setPadding(0, 0, 0, getResources().getInteger(R.integer.text_view_padding));

您也可以在其他布局中使用(xml 文件)。

android:padding="@dimen/text_view_padding"

@Jesús Castro 你做得对。在 dimens.xml 文件中维护值比在所有布局文件中乱扔硬编码值要好。

例如,想象一下您在所有视图中增加左右边距的情况。如果您使用 dimens.xml 中维护的单个值,这将是一个快速更改 - 单个文件中的单个值。 但是,如果您在布局文件中将边距值作为文字值(例如“16dp”)(而不是使用像“@dimen/leftright_margin”这样的尺寸值),则必须编辑每个布局文件容易出错,而且很耗时。

您不需要在值文件夹文件中提及维度值。这个库自动管理你调用的所有东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/_20sdp"
android:paddingLeft="@dimen/_20sdp"
android:paddingRight="@dimen/_20sdp"
android:paddingTop="@dimen/_20sdp"
android:background="@color/colorPrimary"
android:orientation="vertical"
 >

whole code click here for that

如何使用dimens.xml

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

  2. 添加 dimen 名称和值。

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

    值可以是 dppxsp

  3. 使用xml

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

    或在代码中

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

何时使用dimens.xml

感谢 this answer 提供更多想法。

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

  2. 支持多个屏幕 - 8dp 的填充在 phone 上可能看起来不错,但在 10" 平板电脑上很糟糕。您可以创建多个 dimens.xml 以用于不同的屏幕。这样您就可以为 phone 设置 8dp,为平板电脑设置 64dp。要创建另一个 dimens.xml 文件,右键单击您的 res 文件夹并选择 新建 > 值资源文件。(详见 this answer

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

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

    int 的这个:

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

我在 my fuller answer 中给出了更多关于如何做这些事情的细节。

什么时候不使用dimens.xml

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

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

我有一个新方法,我认为它与问题保持一致。我一直在避免使用 Xml 以避免解析 xml 代码的成本。

我没有使用 xml 维度,而是使用 java 常量。 或者...

public interface DimenConstants { ... }

或...

public class DimenConstants
{
    public static void init(Activity activity){...}
}

那么在支持不同屏幕的情况下,你其实可以在Java运行时自己做。一种方式是:

public class TestScreenSizes extends Activity
{
    public static final ViewGroup.LayoutParams MAIN_VIEW_SPEC = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

    @Override protected void onCreate(Bundle savedState)
    {
     super.onCreate(savedState);
     setContentView(newTextView(),MAIN_VIEW_SPEC);
    }

    protected TextView newTextView()
    {
     TextView tv = new TextView(this);
     DisplayMetrics display = getResources().getDisplayMetrics();

     int resolution = display.widthPixels * display.heightPixels;

     if(resolution == 1024) tv.setText("You are using an iphone");
     else if(resolution == 4096) tv.setText("You are using a Samsung Galexy");

     return rv;
    }
}