Add state wings to Android layout

Source: Internet
Author: User

In Android, it is easy to add state to the appearance of a view, which reflects the behavior states such as activate, select, hover, and so on. However, for the dynamic adjustment of the layout of the view, such as a certain state, the need to display a button, the next state, do not need to display this button, it seems pale.

This situation, people will say I go to change the visibility of the button is ok ah. That's true, but isn't it a hassle when it comes to multiple states and multiple controls? The main problems of existing solutions are:

(1) Multi-state, multi-component difficult to manage.

(2) Useless components are just invisible, but still exist, wasting resources.

Is there a better way? Yes, that's the introduction of state.

Look first:

This is the default state:

Click the Show clicked button to change to:

Finally, click Show All to switch to the all state:

There are two components: Buttons and text boxes, three states: default, clicked, all. The default display text box, clicked under the Display button, all under Show all.

How do you do that?

First you need to define the state

Define all states with Tractor:states on the layout of the State view, tractor:currentstate specify the default state. As shown in the following code:

< LinearLayout          xmlns:android = "Http://schemas.android.com/apk/res/android"     xmlns:tractor= "Http://schemas.tractorframework.com/tractor"...    Tractor:states= "Default,clicked,all"    tractor:currentstate= "Default"     tractor:layout= "Activity_main">       

There is also a label trantor:layout, which is used to specify the layout of the state view, in the state switch, the need for this parameter to get layout, reconstruct the entire view. This means that the layout of the State view requires

A separate layout file to define. A bit small inconvenient, but for a lot of visibility management, can accept, you say?

Declaring a corresponding state on a control

Use Tractor:includein on the view control to indicate which states the control exists in, or use Tractor:excludefrom to specify which states do not exist. The following is a status declaration of the two controls for the example program:

 <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Tractor:includein= "Clicked,all"Android:text= "Hello clicked State"        />    <EditTextAndroid:id= "@+id/edittext"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Tractor:excludefrom= "clicked"Android:text= "Hello default state" />

The button exists in Clicked,all two states, and the text box does not exist in the clicked State, that is, the default and all states exist.

State switching

It's all about definitions and declarations, isn't it simple? State switching is also not complex, in the above example, the top three buttons are switching state, they are clicked, will switch to the corresponding state. For example, Show clicked button click will go to switch to the clicked State, the code is this:

     Public void switchtoclicked (View v)    {        = Findviewbyid (r.id.root);        Sf.getviewstate (Root). Setcurrentstate ("clicked");    }

First get the state view, next get the state to try to correspond to the viewstate to set the current state. Everything is easy to operate, but everyone will say what SF is, see below.

The core is to join the state support , before the activity set up the view, re-set Layoutinflater Factory2 factory, this in the SDK version 11 only Oh. This SF is a Factory2 factory instance that has been added to the state support.

The activity of the example program is structured like this:

  @Override  protected  void   OnCreate (Bundle savedinstancestate) { super  .oncreate (Savedinstancestate);        Createinflator ();    Setcontentview (R.layout.activity_main);  private  void   Createinflator () {layoutinflater Inflater  = layoutinflater.from (this);        SF  = new   Statefactory (inflater);    Inflater.setfactory2 (SF); }

The core code is statefactory. This place can download the entire example program, which has statefactory code. The whole example is just a demo, and there's something wrong with statefactory implementation, but we can see the possibility that Android supports view state. With the State,android View code will be good to write a lot, all the goals: simple. What do you say?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.