在Android開發(fā)中,tools
屬性是用來在設(shè)計(jì)時(shí)輔助開發(fā)的屬性,不會(huì)被編譯到最終的APK文件中。設(shè)置tools
屬性可以幫助開發(fā)者在布局文件中進(jìn)行預(yù)覽和調(diào)試。
常用的tools
屬性有:
tools:text
:設(shè)置TextView等控件在設(shè)計(jì)時(shí)顯示的文本內(nèi)容。<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Hello World" />
tools:listitem
:設(shè)置RecyclerView等列表控件中每個(gè)item的預(yù)覽布局。<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_list" />
tools:src
:設(shè)置ImageView等圖片控件在設(shè)計(jì)時(shí)顯示的圖片資源。<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:src="@drawable/sample_image" />
設(shè)置tools
屬性的方法和設(shè)置普通屬性的方法一樣,只需要在對(duì)應(yīng)的控件上添加tools
前綴即可。在編譯時(shí),Android Studio會(huì)忽略這些屬性,只在設(shè)計(jì)時(shí)起作用。