The code address is as follows:
Http://www.demodashi.com/demo/12612.html
Objective
Android
Development is inevitable is UI
the preview and adjustment, in general, is directly run
see the effect, or the use AS
of the preview
preview, but this also brings a small problem, that is, your test content will follow the code is packaged into apk
the If you do not do fault-tolerant processing, it is likely that the test content will be presented to the user.
There are some lists that do not support the preview effect, for example ListView
.
In fact, the Android
old has a tools
namespace, as a development fast to a year, I recently saw an article found this tools
big use, here to make a note.
Look directly at the effect, the most clear:
XML
The code in a few lines, but the right side of the preview
effect is very direct, there is that the data is not hit apk
, direct operation is not any effect, this is the point! The relevant code has been submitted to Github
: Android Tools
Project Files Directory
Preliminary use
Since it is a namespace, it must be XML
declared at the beginning.
xmlns:tools="http://schemas.android.com/tools"
Similar to
xmlns:android="http://schemas.android.com/apk/res/android"
Then you can use tools
the related properties. Property features a lot of very full, I will introduce the commonly used, the end of the text will be affixed to the relevant article links.
View related
First of all, from view
the relevant, in XML
view
the android
value of any attribute can be directly replaced tools
, so that you can achieve real-time preview, and will not show after the formal deployment. A bit similar to dataBinding
the default
attributes in, TextView
for example.
//1. 原生android:text="test text"//2. databindingandroid:text=‘@{viewModel.content, default="test text"}‘//3. toolstools:text="test text"
preview
The effect is the same, but in addition to 1, the other run effect is determined by the content set in the code, really do the test preview.
Previously also said, view
the other properties it supports.
Context
Pass
tools:context="com.xiaweizi.androidtoolsexample.CommonActivity"
The way in which the root office is referenced, the primary function declares that the layout file is associated by default Activity
, and the Activity
full package name of the quoted class is passed. You can then turn on some of the features associated with it. For example, in a daily click event, you can directly XML
declare onClick
the method name in, and then there will be the wrong lint
hint, you need to create a change in the Activity
method, followed AS
by the smart guidance, you can create the corresponding method. Click on the event's handling to put it inside.
Showin
tools:showIn="@layout/activity_other"
When used include
or merge
, the inner layout is not previewed to the outer layer of the layout. It showIn
can be displayed directly with the outer layout by linking to the outer layout.
Layout
Also in the XML
reference fragment
layout is not preview the effect:
That through:
<fragment android:id="@+id/fragment" android:name="com.xiaweizi.androidtoolsexample.CommonFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/layout_fragment_test" />
To achieve the preview effect.
Advanced use
tools
Apart from the above features, the more powerful is the support list of the preview display, that is, the effect of the beginning of the article preview, the most obvious effect is ListView
.
Listview
// 1. 列表内容的布局tools:listitem="@layout/list_content"// 2. 头布局的预览展示tools:listheader="@layout/list_header"// 3. 脚布局的预览展示tools:listfooter="@layout/list_footer"
When you start using it, you find it doesn't work, and then you find that you need to set it up ListView
id
and it takes effect immediately.
Gridview
The same applies to GridView
.
Recyclerview
Speaking of RecyclerView
the first I thought that only supported itemCount
the settings, and later thanks to the small partners to mention the ANDROIDTOOLSEXAMPLE/ISSUES/1, I found that can support complex list display!
// 1. 设置展示 item 的数量tools:itemCount="6"// 2. 设置布局方式 三种模式可以选择// GridLayoutManager// LinearLayoutManager// StaggeredGridLayoutManagertools:layoutManager="GridLayoutManager"// 3. item 的布局tools:listitem="@layout/list_content"// 4 设置布局的方向tools:orientation="horizontal"// 5. 设置布局横、纵的列数tools:spanCount="2"
Sample
Some people will be curious about the list of data which came, did not see you set up, unexpectedly can do every piece of data is different. Sir Don't worry, the next step is to introduce @tools:sample/*
the powerful features as placeholder data for the preview view. See the introduction of the use of a crossing net.
Various types of data are supported for construction, and here I take the example as a showcase:
Each time you use it, you will randomly change the values, see the list for more information.
Do you think it's over? too young
! We can also construct false data by ourselves, except for the data support that is natively given.
You can create sample data
it directly, and then you can create text or json
data. If it is json
data, you must run it before you can use it. Then look at how to use it.
Create the json
data first:
{ "data": [ { "name": "张三", "phone": "@tools:sample/us_phones", "time": "@tools:sample/date/hhmmss", "avatar": "@tools:sample/avatars" }, { "name": "李四", "phone": "@tools:sample/us_phones", "time": "@tools:sample/date/hhmmss", "avatar": "@tools:sample/avatars" }, { "name": "赵五", "phone": "@tools:sample/us_phones", "time": "@tools:sample/date/hhmmss", "avatar": "@tools:sample/avatars" }, { "name": "王二麻", "phone": "@tools:sample/us_phones", "time": "@tools:sample/date/hhmmss", "avatar": "@tools:sample/avatars" } ]}
Then directly in XML
, through @sample/
the way to get the data.
Use and preview as shown.
Summarize
get
This skill, do not say how much use it, at least can improve the development efficiency, and reduce the test code to deploy to the wrong rate of the line, I believe it will help you!
Thank
Tools Official Documentation
namespace Introduction
Tools are useful.
Android Cold weapons tools
The code address is as follows:
Http://www.demodashi.com/demo/12612.html
Note: This copyright belongs to the author, by the demo master, refused to reprint, reprint need the author authorization
Android Cold weapons tools