2013-12-15 177 views
0

@string我有以下XML代碼:TextView的TEXTSIZE和Android的

<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顯示在看起來很尷尬的文本中。這是什麼原因呢!

回答

1

硬編碼StringView中的值不被developer.android.com推薦,因爲使得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:

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

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

這種佈局XML應用將一個字符串查看:

<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設置size通過developer.android.com

的建議

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.

保存在RES /值/ dimens.xml

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

的Android旁觀者TextView的大小是使用SP和你硬編碼字符串,我給予警告。

請將您的String.xml放在value文件夾中並選擇此字符串,然後它不會給出任何錯誤。

感謝

0

這是更好to use SP instead of DP

建議始終使用字符串資源文件,因爲如果您需要更改多個xml文件中使用的單個@String,則必須只更改一次。反之亦然,如果您在xml佈局文件中編寫字符串,如果您需要更改字符串,則需要搜索字符串,搜索xml文件,然後更改所需的許多常量。

總之,硬編碼字符串不是一個好習慣。您應該將它們指定爲字符串資源文件,然後在您的佈局中引用它們。通過編輯strings.xml文件,您可以在同一時間通過更新每個出現的單個單詞的所有單詞

它也需要支持多種語言定義爲單獨的strings.xml一種語言的文件!