2013-12-15 179 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一种语言的文件!