Objective
In Android development, the IDE can see the layout preview as you write the layout code.
However, some effects must be run before they can be seen, such as when the TextView does not set any characters in the XML, but instead sets the text in the activity. So in order to preview the effect in the IDE, you must set the property for the TextView control in the XML android:text
<textview
android:id= "@+id/text_main"
android:layout_width= "match_parent"
android:layout_height = "Wrap_content"
android:textappearance= @style/textappearance.title "
android:layout_margin=" @dimen Main_margin "
android:text=" I am a title "/>"
In general, we tell ourselves when we do this, it doesn't matter, I'll delete the items as soon as I have finished the code. But you may forget that there is such code in your final product.
Use tools, don't do anything stupid.
The above situation can be avoided, we use the Tools namespace and its properties to solve this problem.
Xmlns:tools=http://schemas.android.com/tools
Tools can tell Android Studio which properties are ignored when they are run and are only valid when designing layouts. Let's say we want android:text
the property to be valid only in Layout preview.
<textview
android:id= "@+id/text_main"
android:layout_width= "match_parent"
android:layout_height = "Wrap_content"
android:textappearance= @style/textappearance.title "
android:layout_margin=" @dimen Main_margin "
tools:text=" I am a title "/>"
Tools can overwrite all of Android's standard properties and will be android:
replaced tools:
. At the same time, even when running tools: itself is ignored, will not be brought into the APK.
Types of tools attributes
The tools attribute can be divided into two types: one that affects lint hints and one that is designed for XML layouts. This is the most basic usage of tools: The second is to overwrite the standard Android properties when UI is designed. The following describes lint related properties.
Lint-related properties
Tools:ignore
Tools:targetapi
Tools:locale
Tools:ignore
The Ignore property is to tell lint to ignore some warnings in the XML.
Suppose we have such a imageview
<imageview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android: layout_marginstart= "@dimen/margin_main"
android:layout_margintop= "@dimen/margin_main"
android: Scaletype= "center"
android:src= "@drawable/divider"/>
Lint prompts the ImageView to be missing a android:contentDescription
property. We can use tools:ignore
to ignore this warning:
<imageview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android: layout_marginstart= "@dimen/margin_main"
android:layout_margintop= "@dimen/margin_main"
android: Scaletype= "center"
android:src= "@drawable/divider"
tools:ignore= "ContentDescription"/>
Tools:targetapi
Suppose Minsdklevel 15, and you use the controls in api21 like Rippledrawable
<ripple xmlns:android= "http://schemas.android.com/apk/res/android"
android:color= "@color/accent_color" >
The lint will prompt for a warning.
To not display this warning, you can:
<ripple xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:color=" @color/accent_color "
tools:targetapi=" Lollipop "/>"
Tools:locale (local language) properties
By default, the string in Res/values/strings.xml performs a spell check, and if it is not in English, prompts for a spelling error, and the following code tells the studio that the local language is not English and there is no hint.
<resources
xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
tools:locale= It" >
<!--Your strings go-->
>
This first describes the basics of tools-covering the properties of Android and then introducing the properties that ignore the lint hint. We'll go on to the other properties of the UI preview (non-Android standard properties) below.
Note: It doesn't matter if you don't want to know about ignoring lint properties, because it doesn't affect compilation, and I don't normally care about these warnings.
In this section we will continue to introduce other properties about the UI preview (non-Android standard properties).
- Tools:context
- Tools:menu
- Tools:actionbarnavmode
- Tools:listitem/listheader/listfooter
- Tools:showin
- Tools:layout
Tools:context
The context attribute is actually the name of the activity attribute, and with this attribute, the IDE knows what topic to use when previewing the layout. He can also help find relevant files in the Java Code of Android Studio (go to Related files)
The value of this property is the full package name of the activity
<linearlayout
xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:id=" @+id/container "
android:layout_width=" Match_parent "
Android : layout_height= "match_parent"
android:orientation= "vertical"
tools:context= " Com.android.example.MainActivity "> <!--...-->
</LinearLayout>
Tools:menu
Tell the IDE which menu to use in the preview window, which will appear on the root node of the layout (Actionbar location).
In fact, the preview window is very intelligent, and if the layout is associated with an activity (referring to the associated with the above tools:context
) it will automatically query the onCreateOptionsMenu
code in the method of the activity concerned to display the menu. The Menu property can override this default behavior.
You can also define multiple menu resources for the menu property, separated by commas between different menu resources.
tools:menu= "Menu_main,menu_edit"
If you don't want the menu to appear in the preview:
Finally, note that Theme.AppCompat
This property does not work when the theme is.
Tools:actionbarnavmode
This property tells the IDE's display mode for Actionbar in app Bar (material), and its value can be
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:orientation=" vertical "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
tools:actionbarnavmode= "tabs"/>
Similarly, when the theme is Theme.AppCompat (r21+, at least)
either Theme.Material
, or uses a layout that contains a toolbar way. This property also does not work, only the Holo topic is valid.
ListItem, Listheader and Listfooter properties
As the name implies ListView ExpandableListView
, you add the head tail and the preview layout of the child item in the preview effect.
<gridview
android:id= "@+id/list"
android:layout_width= "Match_parent"
Wrap_content "
tools:listheader=" @layout/list_header "
tools:listitem=" @layout/list_item "
tools: listfooter= "@layout/list_footer"/>
Layout property
tools:layout
Tell Ide,fragment what to show when the program is previewed
<fragment xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:id=" @+id/item_list "
android:name=" Com.example.fragmenttwopanel.ItemListFragment "
android:layout_width=" match_parent "
android:layout_ height= "Match_parent"
android:layout_marginleft= "16DP"
android:layout_marginright= "16DP"
tools: Layout= "@android: Layout/list_content"/>
Tools:showin
This property is set on a root element of a layout that is <include> by another layout. This allows you to point to one of the layouts that contain this layout, and the included layout is rendered at design time with the surrounding external layout. This will allow you to view and edit this layout "in context." Requires Studio 0.5.8 or later.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.