“Hardcoded string "xxx", should use @string resource”警告

来源:互联网 发布:mac sacd 编辑:程序博客网 时间:2024/06/06 03:07

警告信息“Hardcoded string "xxx", should use @string resource”的消除方法

  问题描述:在xml中添加Button控件,控件信息如下。

              android:id="@+id/mButton_mc"
         android:text="mc"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"            
     

  编译时,提示“Hardcoded string "mc", should use @string resource”警告。

  原因分析:在android:text中使用到了字符串mc,应该将该字符串定义在String.xml中,然后再通过调用String.xml中该字符串的资源名来使用该字符串资源。这样做的好处在于可以做到一改全改,并且在支持多语言时也是很有用处的。

  解决方法:在项目目录下的res-->values-->String.xml中添加字符串mc的信息如下。

  
      mc

  

  然后,再在使用该Button控件的xml中,通过调用该字符串的资源名来使用该字符串,如下。

                android:id="@+id/mButton_mc"
          android:text="@string/mc"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"  

0 0
原创粉丝点击