"Android Basics" "selector" "Custom Controls"

Source: Internet
Author: User

Project Demo Source: http://download.csdn.net/detail/mcdullsin/8291231


Although I am still a more than a study of new people, but sincerely hope that all the blog is attached to the source code, such as I can shorten the learning distance.

Effect:

Well, first look at a selector selector.

Btn.xml

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android" >     <Itemandroid:state_focused= "true"android:state_pressed= "false"android:drawable= "@drawable/btn_normal"></Item>    <Itemandroid:state_pressed= "true"android:drawable= "@drawable/btn_white"></Item>    <Itemandroid:state_checked= "true"android:drawable= "@drawable/btn_white"></Item>    <Itemandroid:state_focused= "false"android:state_pressed= "false"android:drawable= "@drawable/btn_normal"></Item>     </selector>

Components that use selector:

< cn.com.karl.view.ImageBtn         Android:id = "@+id/btn_right"         android:layout_height= "Wrap_content"         android:layout_width= "Wrap_ Content "        android:background=" @drawable/btn "         />

Use it as a static picture.

This selector defines the background of the button picture in four states, namely: no focus is not clicked, the focus is not clicked, clicked, selected. Different state slices have different backgrounds. It is best to have an initial picture using Selector. I said this to the button, there may be a different listview? No further tests have been conducted.

A very good blog post about selector and shape: http://blog.csdn.net/tianjf0514/article/details/7492876

Second, custom controls

1. First define a layout to implement the interior of the button:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" >     <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:paddingbottom= "5dip"Android:layout_marginleft= "8DP"Android:paddingtop= "5dip" />     <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_marginleft= "8dip"Android:text= "OK"Android:layout_marginright= "8DP"Android:textcolor= "#000000" /> </LinearLayout>

2, next write a class inherit linearlayout, import just the layout, and set the required method (set text, set the method of the picture), so that the code can control the display of the content of the custom control.

 Public classImagebtnextendsLinearLayout {PrivateImageView ImageView; PrivateTextView TextView;  Publicimagebtn (Context context) {Super(context); //TODO auto-generated Constructor stub   }    Publicimagebtn (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stubLayoutinflater inflater=(Layoutinflater) Context.getsystemservice (Context.layout_inflater_service); Inflater.inflate (R.LAYOUT.IMAGEBTN, This); ImageView=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW1); }       /*** Set up Picture resources*/     Public voidSetimageresource (intresId)    {Imageview.setimageresource (resId); }      /*** Set the displayed text*/     Public voidSettextviewtext (String text) {textview.settext (text); } }

In the above code, it is important to:

layoutinflater inflater= (layoutinflater) Context.getsystemservice (Context.layout_inflater_service); Inflater.inflate (R.LAYOUT.IMAGEBTN,this); ImageView=(ImageView) Findviewbyid (r.id.imageview1); TextView= (textView) Findviewbyid ( R.ID.TEXTVIEW1);

They are then referenced in the main layout file and found in the activity, adding events.

Main layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" > <cn.com.karl.view.ImageBtn Android:id= "@+id/btn_right"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:background= "@drawable/btn"/> <cn.com.karl.view.ImageBtn Android:id= "@+id/btn_error"Android:layout_marginleft= "5DP"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:background= "@drawable/btn"/> </LinearLayout>

Java files:

 Public classMainactivityextendsActivity {Privateimagebtn imageBtn1; Privateimagebtn imageBtn2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub       Super. OnCreate (savedinstancestate);               Setcontentview (R.layout.activity_main); IMAGEBTN1= (IMAGEBTN) This. Findviewbyid (R.id.btn_right); IMAGEBTN2= (IMAGEBTN) This. Findviewbyid (R.id.btn_error); Imagebtn1.settextviewtext (Determine); Imagebtn2.settextviewtext (Cancel);       Imagebtn1.setimageresource (R.drawable.right_icon);               Imagebtn2.setimageresource (R.drawable.error_icon); Imagebtn1.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (Getapplicationcontext (), "Click on the Right button", 1). Show ();               }       }); Imagebtn2.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (Getapplicationcontext (), "clicked Error Button", 1). Show ();   }       }); }}

"Android Basics" "selector" "Custom Controls"

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.