永远不会使用命名空间声明:检查XML中未使用的命名空间声明和位置提示

时间:2015-05-20 02:21:25

标签: java android ios date android-tools-namespace

  xmlns:tools="http://schemas.android.com/tools" 

这个是我的问题,据说"名称空间声明从未使用"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="josh appear here"
    android:id="@+id/josh_text_view"
    />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="open Second Activity"
    android:layout_below="@+id/josh_text_view"
    android:onClick="showJosh"
    android:id="@+id/button" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="open Third Activity"
    android:onClick="showJosh"
    android:id="@+id/button2"

并且在每个按钮中都会出现始终弹出的警告

[I18N]硬编码字符串&#34;打开第二个活动&#34;,应该使用@string资源少...(Ctrl + F1) 直接在布局文件中硬编码文本属性有几个原因:

  • 创建配置变体时(例如横向或纵向),您必须重复实际文本(并在进行更改时使其保持最新)

  • 仅通过为现有字符串资源添加新翻译,无法将应用程序翻译为其他语言。在Android Studio和Eclipse中,有一些快速修正工具可以自动将这个硬编码字符串提取到资源查找中。

            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="10dp" />
    
            <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="open Fourth Activity"
            android:onClick="showJosh"
            android:id="@+id/button3"
    
            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="75dp" />
    
    </RelativeLayout>
    

1 个答案:

答案 0 :(得分:0)

Namespace declaration is not used不是错误。这只是一个警告。如果您愿意,可以删除该行,但您可以不管它。关于@string资源的使用,最好在strings.xml文件中定义字符串值并在项目中引用它们。 strings.xml文件位于res&gt; values文件夹中。例如:在strings.xml文件中,应该已经存在一些字符串资源。将以下行添加到文件

<string name="openThird">open Third Activity</string>

现在,在布局文件中,执行此操作

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/openThird"
android:onClick="showJosh"
android:id="@+id/button2"

对其他按钮执行相同操作。在strings.xml中定义字符串值,然后在布局中使用它们。