簡體   English   中英

如何在Android中使用dimens.xml?

[英]How to use dimens.xml in Android?

當我設計布局時,出於可維護性的考慮,我將所有維度集中在 dimens.xml 中。 我的問題是這是否正確。 最好的做法是什么? 關於這方面的信息很少,沒有。 我知道將布局的所有字符串集中在 strings.xml、colors 和 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" />

如何使用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

感謝這個答案提供更多想法。

  1. 重用值- 如果您需要在整個應用程序中的多個位置使用相同的維度(例如,Activity 布局填充或 TextView textSize ),那么使用單個dimen值將使以后更容易調整。 這與使用樣式和主題的想法相同。

  2. 支持多屏幕- 8dp的內邊距在手機上可能看起來不錯,但在 10" 平板電腦上可能很糟糕。您可以創建多個dimens.xml以用於不同的屏幕。這樣您就可8dp手機設置8dp64dp用於平板電腦。要創建另一個dimens.xml文件,請右鍵單擊您的res文件夾並選擇“新建”>“值資源文件” 。(有關詳細信息,請參閱此答案

  3. 方便的dppx代碼轉換- 在代碼中,您通常需要處理像素值。 但是,您仍然必須考慮設備密度,並且以編程方式進行轉換很煩人。 如果你有一個恆定的dp值,你可以像這樣簡單地以像素為單位得到它float

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

    或者這對於int

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

我在更完整的答案中提供了更多關於如何做這些事情的細節。

何時不使用dimens.xml

  • 不要將您的值放在dimens.xml如果這會使它們更難以維護。 一般來說,只要它不屬於我上面列出的類別。 使用dimens.xml會使代碼更難閱讀,因為您必須在兩個文件之間來回切換才能查看實際值。 個人觀點不值得(在我看來)。

  • 字符串不一樣。 所有字符串都應該放在像strings.xml這樣的資源文件中,因為在國際化應用程序時幾乎所有的字符串都需要翻譯。 另一方面,大多數維度值不需要針對不同的位置進行更改。 Android Studio 似乎支持這種推理。 直接在布局 xml 中定義字符串會給出警告,但定義dp值不會。

添加一個 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"

您不需要在值文件夾文件中提及維度值。 這個庫會自動管理你剛才調用的所有東西

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

整個代碼點擊這里

But about dimensions?

根據Android官方文檔“維度是使用name屬性中提供的值(而不是XML文件的名稱)引用的簡單資源。因此,您可以將維度資源與一個XML中的其他簡單資源結合起來文件,在一個<resources>元素下”

有關更多詳細信息,請參閱http://developer.android.com/guide/topics/resources/more-resources.html

在這篇文章中,Devunwired 給出了為什么使用 dimens.xml 的三個重要原因,什么時候應該在 Android 中使用 dimens.xml 文件?

@Jesús Castro 你做得對。 在 dimens.xml 文件中維護值比在所有布局文件中亂扔硬編碼值要好。

例如,想象一下您在所有視圖中增加左右邊距的情況。 如果您使用在 dimens.xml 中維護的單個值,這將是一個快速更改 - 單個文件中的單個值。 但是,如果您在布局文件中將邊距值作為文字值(例如“16dp”)(而不是使用像“@dimen/leftright_margin”這樣的尺寸值),則必須編輯每個容易出錯的布局文件而且很費時間。

我有一種新穎的方法,我認為它與問題保持一致。 我一直在避免使用 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;
    }
}

yes absolutely 最好將值保留在 dimens.xml 文件中

我不知道它是否可以幫助你,但我寫了一個小的 java 程序,它允許你用一個新的期望值復制一個維度 xml 文件,這樣你就不再需要逐行手工完成。

https://github.com/Drex-xdev/Dimensions-Scalable-Android

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM