有没有一种方法可以消除布局文件中关于硬编码字符串的个别警告?
我经常在TextView中放置占位文本,以便在设计时查看它们。这样做的缺点是会收到大量关于硬编码字符串的警告。但如果没有这些警告,我就无法在布局中看到TextView。
您可以将以下内容添加到文本视图元素中:
tools:ignore="HardcodedText"
示例:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a hardcoded text"
tools:ignore="HardcodedText" />
请注意,Eclipse中有一个快捷方式可以轻松添加此内容:只需按下CTRL + 1并选择相关选项。
不幸的是,我找不到一种方法来对整个布局执行此操作,您将需要为每个元素执行此操作。
请注意,您还必须将xmlns:tools="http://schemas.android.com/tools
属性添加到根元素中。
tools:text
而不是android:text
:
另一种方法是使用tools:text
而不是 android:text
:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="This is a hardcoded text" />
请注意,您还必须将xmlns:tools="http://schemas.android.com/tools
属性添加到根元素。tools:ignore="HardcodedText"
更好,因为预览实际上会显示它。 - Shine使用 string.xml
文件来消除此警告...
您需要将字符串放入 string.xml 文件中,然后像这样设置 android:text="@string/mytext"
在 res-->value->string.xml
中添加 <string name="mytext">Your Text</string>
strings.xml
的原则,但有时你知道你只使用该字符串一次。 - Jacksonkr