Android Technology 15: Custom Control implementations

Source: Internet
Author: User

In Android development, common components sometimes do not meet our needs, so we need to customize components, so that can improve the reusability of components, by inheriting the existing components, on the basis of the tower improvement, the following demonstrates a simple one button control, The tower consists of 2 ImageView and one textview.

1. Component templates

Mybutton.xml

1 <LinearLayout2     xmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent" 4 Android:layout_height= "Wrap_content"5 android:padding= "10dip"6 android:orientation= "Horizontal">7     <ImageView8         Android:id= "@+id/icon"9 Android:layout_width= "0dip"Ten Android:layout_height= "Wrap_content" One Android:layout_weight= "1" A android:src= "@drawable/icon" - android:layout_gravity= "Left|center_vertical" -      /> the      <TextView -          Android:id= "@+id/text" - Android:layout_width= "0dip" - Android:layout_height= "Wrap_content" + Android:layout_weight= "3" - android:layout_gravity= "Left|center_vertical" + android:textsize= "18SP" A Android:text= "button text"/> at       <ImageView -         Android:id= "@+id/arrow" - Android:layout_width= "0dip" - Android:layout_height= "Wrap_content" - Android:layout_weight= "1" - android:layout_gravity= "Center_vertical" in android:src= "@drawable/arrow" -      /> to </LinearLayout>

2. Inheriting the parent component

Mybutton.java

1  Public classMyButtonextendslinearlayout{2 3     PrivateImageView Imgaeview;//icons4     PrivateTextView TextView;//text5     PrivateImageView Arrow;//Arrows6      PublicMyButton (Context context) {7         Super(context);8         9     }Ten      PublicMyButton (Context context, AttributeSet attrs) { One         Super(context, attrs); A Initview (context); -     } -     Private voidInitview (Context context) { theLayoutinflater inflater=(Layoutinflater) context.getsystemservice (context. Layout_inflater_service); -LinearLayout linearlayout= (linearlayout) inflater.inflate (R.layout.mybutton, This); -imgaeview=(ImageView) Linearlayout.findviewbyid (R.id.icon); -textview=(TextView) Linearlayout.findviewbyid (r.id.text); +arrow=(ImageView) Linearlayout.findviewbyid (r.id.arrow); -     } +     //Modify Icon A      Public voidSetimageviewresource (intresId) { at Imgaeview.setimageresource (resId); -     } -     //Modify Text -      Public voidSettextviewtext (String text) { - textview.settext (text); -     } in      -      to      +}

3.Layout Adding components

< Com.forsta.weight.MyButton          Android:id = "@+id/bt1"         android:layout_width= "Fill_parent"        android:layout_height= "Wrap_ Content "        android:background=" @drawable/bg "/>

4. Define the background

Bg.xml

1 <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">2     <Itemandroid:state_pressed= "true"android:drawable= "@drawable/bg2"></Item>3     <Itemandroid:state_pressed= "false"android:drawable= "@drawable/bg1"></Item>4 </selector>

Bg1.xml

1 <  xmlns:android= "http://schemas.android.com/apk/res/android">2     <android:startcolor= "#FFFFFF"  android:endcolor = "#8DEEEE" /> 3 </ Shape >

Bg2.xml

1 <  xmlns:android= "http://schemas.android.com/apk/res/android">2     <android:startcolor= "#EEE685"  android:endcolor= "#DDA0DD"></gradient>3</shape  >

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.