Android custom controls (5) custom composite controls

Source: Internet
Author: User
Tags list of attributes custom name

Turn from http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html code to write for yourself

Objective: To combine textview and imagebutton. You can set attributes of custom controls in XML.

Set custom control attributes through code or XML

1. control layout: Use linearlayout as the root layout, one textview and one imagebutton.

  

XML Code
< ?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:gravity="center_vertical">      < TextView android:layout_height="wrap_content" android:id="@+id/text1"      android:layout_width="wrap_content">< /TextView>      < ImageButton android:layout_width="wrap_content"      android:layout_height="wrap_content" android:id="@+id/btn1">< /ImageButton>      < /LinearLayout>

2. custom control code, inherited from linearlayout:

  

Java code
Public class imagebtnwithtext extends linearlayout {} public imagebtnwithtext (context) {This (context, null);} public imagebtnwithtext (context, attributeset attrs) {super (context, attrs ); // parse the layout defined in XML in the constructor. Layoutinflater. From (context). Inflate (R. layout. imagebtn_with_text, this, true );}}

 

3. Use custom controls in the main interface layout XML:

  

XML Code
< com.demo.widget2.ImageBtnWithText     android:id="@+id/widget"     android:layout_width="fill_parent"     android:layout_height="fill_parent" />

Use the complete custom control class Path COM. Demo. widget2.imagebtnwithtext to define an element.

Run the command to see that the control can be loaded to the interface.

4. Set the image and text for the button

If the image is fixed, you can directly set the src attribute of imagebutton in the control layout.

4.1 set through Java code and provide function interfaces in the control code:

  

Java code
public void setButtonImageResource(int resId) {     mBtn.setImageResource(resId);     }         public void setTextViewText(String text) {     mTv.setText(text);     }

Then you can set it through the function call in oncreate () on the main interface.

4.2 set attributes through XML

4.2.1 first define the set of attributes that can be set in XML. Create attrs. xml under values. The file name is optional and generally called attrs. xml.

  

XML Code
< ?xml version="1.0" encoding="utf-8"?>    < resources>     < declare-styleable name="ImageBtnWithText">     < attr name="android:text"/>     < attr name="android:src"/>     < /declare-styleable>     < /resources>

Attribute Set Name: imagebtnwithtext, which can be defined based on actual conditions;

The list of attributes contained in the collection. Each row has one attribute.

4.2.2 modify the custom control implementation code to get the attributes defined in XML

  

Java code
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageBtnWithText);    CharSequence text = a.getText(R.styleable.ImageBtnWithText_android_text);    if(text != null) mTv.setText(text);    Drawable drawable = a.getDrawable(R.styleable.ImageBtnWithText_android_src);    if(drawable != null) mBtn.setImageDrawable(drawable);    a.recycle();

First, use context. obtainstyledattributes to obtain all attribute arrays;

Then, the corresponding values are obtained through the getxxxx () series interface of the typedarray class.

4.2.3 set the owner of the custom control in the main interface Layout

Android: text = "ABC" Android: src = "@ drawable/icon

4.3 custom name attributes:

The attribute names used in 4.2 are in the Android system namespace and start with Android, such as Android: text.

In actual development, some property names will be customized. These property names are still defined in attrs. xml mentioned in 4.2.1:

4.3.1 define the attribute name

  

XML Code
< attr name="appendText" format="string"/>

Different from using the system's ATTR, you need to add a format attribute to describe the format of this attribute. For more information about format options, see note 1.

4.3.2 Use Custom Attributes

  

XML Code
< ?xml version="1.0" encoding="utf-8"?>     < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:myspace="http://schemas.android.com/apk/res/com.demo.customwidget"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     < com.demo.widget2.ImageBtnWithText     android:text="ABC" android:src="@drawable/icon" android:id="@+id/widget"     android:layout_width="fill_parent" android:layout_gravity="center"     android:layout_height="fill_parent" myspace:appendText="123456">     < /com.demo.widget2.ImageBtnWithText>     < /LinearLayout>

Http://download.csdn.net/detail/ethan_xue/4109870

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.