Custom drop-down menu Mode

Source: Internet
Author: User
Example

The spinner is the drop-down menu, which is equivalent to the swing combo box and HTML <SELECT>. Due to limited mobile phone screen, you must select a project within a limited range, the drop-down menu is unique and a good choice.

Android
Provided Spinner
The drop-down menu of widgets is very easy to use and the style is also applicable. However, the focus of this example is to customize the style in the drop-down menu. The key is to call
The setdropdownviewresource method defines the display style of the drop-down menu in XML format. In addition to the custom drop-down menu, this example also uses a program to design an animation.
When a user clicks the custom spinner in touch mode, an animation is displayed, prompting the user.

In new
In arrayadapter, we will use arrayadapter (context, int
Textviewresourceid, t []
Objects) This constructor, textviewresourceid uses the resourceid provided by Android, objects is required
String Array to be passed ).

Sample program


Setdropdownviewresource of the adapter allows you to set the display mode of the drop-down menu, and define the XML in the Res/layout directory.
Set the textview in the menu, as in the R. layout. myspinner _
Dropdown is the custom drop-down menu textview style. In addition to changing the style of the drop-down menu, it also performs a dynamic effect on the spinner.
Then, the menu (myanimation) appears ).

/* Import program omitted */

Public class ex04_08 extends Activity
{
Private Static final string [] countriesstr =
{"Beijing", "Shanghai", "Tianjin", "Chongqing "};
Private textview mytextview;
Private spinner myspinner;
Private arrayadapter <string> adapter;
Animation myanimation;

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
/* Load main. xml Layout */
Setcontentview (R. layout. Main );

/* Get the object with findviewbyid */
Mytextview = (textview) findviewbyid (R. Id. mytextview );
Myspinner = (spinner) findviewbyid (R. Id. myspinner );

Adapter = new arrayadapter <string> (this,
Android. R. layout. simple_spinner_item, countriesstr );
/* Myspinner_dropdown: Customize the drop-down menu style in the Res/layout directory */
Adapter. setdropdownviewresource (R. layout. myspinner_dropdown );

/* Add the arrayadapter to the spinner object */
Myspinner. setadapter (adapter );

/* Add onitemselectedlistener to myspinner */
Myspinner. setonitemselectedlistener
(New Spinner. onitemselectedlistener ()
{
@ Override
Public void onitemselected
(Adapterview <?> Arg0, view arg1, int arg2,
Long arg3)
{
/* Bring the value of the selected myspinner to mytextview */
Mytextview. settext ("selected" + countriesstr [arg2]);
/* Display myspinner */
Arg0.setvisibility (view. Visible );
}

@ Override
Public void onnothingselected (adapterview <?> Arg0)
{
// Todo auto-generated method stub
}
});

/* Obtain the animation defined in the Res/anim directory */
Myanimation = animationutils. loadanimation (this, R. anim. my_anim );

/* Add the ontouchlistener to myspinner */
Myspinner. setontouchlistener (new Spinner. ontouchlistener ()
{

@ Override
Public Boolean ontouch (view V, motionevent event)
{
/* Run animation on myspinner */
V. startanimation (myanimation );
/* Hide myspinner */
V. setvisibility (view. Invisible );
Return false;
}

});

Myspinner. setonfocuschangelistener (new Spinner. onfocuschangelistener ()
{
@ Override
Public void onfocuschange (view V, Boolean hasfocus)
{
// Todo auto-generated method stub
}
});
}
}
Res/layout/myspinner_dropdown.xml

Change the XML format of the drop-down menu. The component used in the drop-down menu is textview.

<?xml version="1.0" encoding="utf-8"?> 
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="24sp"
android:singleLine="true"
style="?android:attr/spinnerDropDownItemStyle" />
Res/anim/my_anim.xml

Android animations are composed of four types: Alpha, scale, translate, and rotate. The following custom animations use either of them.

Code<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="300"
>
</translate>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="300">
</alpha>
</set>
Extended learning

Animation mainly has two dynamic modes: tweened animation (gradient animation) and frame by frame animation (screen conversion animation ). Tweened animation has the following four basic conversion methods.

· Alphaanimation (Transparency changes): Transparency conversion.

· Rotateanimation (rotations): rotation conversion.

· Scaleanimation (growing or shrinking): Scaling and conversion.

· Translateanimation (position changes): Location conversion.

After defining the animation XML you want, use animationutils. loadanimation to load the animation and try to use the startanimation method in the component that wants to add dynamic effects.

Http://www.cnblogs.com/alex77lee/archive/2010/08/04/1792454.html

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.