簡體   English   中英

Android中的TextView textSize和@string

[英]TextView textSize and @string in Android

我有以下xml代碼:

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Press Button"                  <!--Warning -->
          android:textSize="45dp"                      <!--Warning -->
          android:layout_gravity="center"
          android:gravity="center"
          android:id="@+id/tvDisplay" />

在xml代碼中,我發現兩個警告,首先是dp包含我確實警告要使用sp警告。 它顯示出來的原因是什么?

第二個警告,可能是錯誤,是我正在使用android:text="Press Button"它告訴我確實使用@string。 如果我使用相同的@string則會在看起來尷尬的文本中顯示。 是什么原因!

Developer.android.com不建議在View硬編碼的String值,因為將與不同語言兼容的Android應用程序制作成標准版本。

引用自

要增加對更多語言的支持,請在res /中創建其他值目錄,在目錄名稱的末尾包含連字符和ISO國家/地區代碼。 例如,values-es /是包含語言代碼“ es”的語言環境的簡單資源的目錄。 Android會在運行時根據設備的語言環境設置加載適當的資源。

確定了支持的語言后,創建資源子目錄和字符串資源文件。 例如:

MyProject/
res/
   values/
       strings.xml
   values-es/
       strings.xml
   values-fr/
       strings.xml

將每個語言環境的字符串值添加到適當的文件中。

在運行時,Android系統根據當前為用戶設備設置的語言環境使用適當的字符串資源集。

例如,以下是用於不同語言的一些不同的字符串資源文件。

英文(默認語言環境),/ values / strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>

西班牙語,/ values-es / strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>

參考您的OP:

XML文件保存在res / values / strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="press">Press Button</string>
</resources>

此布局XML將字符串應用於View:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press"      
android:textSize="45dp"    <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"  />

此應用程序代碼檢索一個字符串:

String string = getString(R.string.hello);

使用sp根據developer.android.com建議設置size

sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

XML文件保存在res / values / dimens.xml中:

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

此應用程序代碼檢索維度

Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);

此布局XML將尺寸應用於屬性:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button"      <!--Warning -->
android:textSize="@dimen/font_size"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay" 
/>

對於文字像素,建議的尺寸類型為“ sp”(例如:15sp)

來自developer.android

Android的標准TextView的大小是使用SP ,你是我給警告的硬編碼字符串。

請在值文件夾中使用您的String.xml並選擇此字符串,則它不會出現任何錯誤。

謝謝

最好使用SP而不是DP

建議使用始終字符串資源文件,因為如果您需要更改多個xml文件中使用的單個@String,則只需更改一次。 反之亦然,如果您在xml布局文件中寫入字符串,則需要更改字符串以搜索該字符串,搜索xml文件,然后更改所需的次數。

總之,對字符串進行硬編碼不是一個好習慣。 您應該將它們指定到字符串資源文件中,然后在布局中引用它們。這使您可以通過僅編輯strings.xml文件來同時更新所有布局中單個單詞的每次出現

還需要支持將多種語言定義作為單獨的strings.xml用於一種語言的一個文件!

暫無
暫無

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

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