安卓开发实用技巧:TextView预览

背景:

     使用TextView时,为了方便在开发工具中预览效果,需要在TextView中设置文字(如:android:text="Hello World"),但是等到后面提交时,为了避免显示这样默认的信息,通常需要把这个删除掉。但是删除后,后续就无法预览TextView了,为开发带来不便。

目标:

既做到可以预览,又不影响TextView的默认显示。

解决方案:

1. 引入命名空间:xmlns:tools="http://schemas.android.com/tools"

2. 为TextView设置:tools:text="Hello World"

样例:

<LinearLayout 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:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:text="Hello World"
        android:textSize="60sp" />
</LinearLayout>

点击下载样例

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/81905584