(ext.) Android Custom Combo control

Source: Internet
Author: User
Tags custom name

Original address: http://mypyg.iteye.com/blog/968646

Goal: Implement the TextView and ImageButton combinations to set the properties of a custom control through XML.
1. Control layout: LinearLayout as root layout, one textview, one ImageButton.

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "fill_parent" android:layout_height="fill_parent "
  4. android:gravity="center_vertical">
  5. <TextView android:layout_height="wrap_content" android:id="@+id/text1 "
  6. android:layout_width="wrap_content"></TextView>
  7. <ImageButton android:layout_width="wrap_content"
  8. android:layout_height= "wrap_content" android:id="@+id/btn1"></ ImageButton>
  9. </linearlayout>


2. Custom control code, inherited from LinearLayout:

Java code
  1. Public class Imagebtnwithtext extends LinearLayout {
  2. Public Imagebtnwithtext (context context) {
  3. This (context, null);
  4. }
  5. Public Imagebtnwithtext (context context, AttributeSet attrs) {
  6. Super (context, attrs);
  7. Layoutinflater.from (context). Inflate (R.layout.imagebtn_with_text, this , true);
  8. }
  9. }


Resolves the layout defined in the XML in the constructor.
PS: Sometimes when you execute this code, eclipse outputs a resource not found, and cannot preview the custom control. The experiment determined that the new layout did not generate resources, but clean project regeneration was not possible, and finally restarted eclipse resolution.
3. Use a custom control in the main interface layout xml:

XML code
    1. <Com.demo.widget2.ImageBtnWithText
    2. android:id="@+id/widget"
    3. android:layout_width="fill_parent"
    4. android:layout_height="fill_parent" />


Even with the full custom control classpath: Com.demo.widget2.ImageBtnWithText defines an element.
Running can see that the control has been able to be loaded onto the interface.
4. Set the picture and text for the button
If the picture is fixed, set the SRC attribute of the ImageButton directly in the control layout.
4.1 A function interface is provided in the control code through the Java Code settings:

Java code
    1. Public void Setbuttonimageresource (int resId) {
    2. Mbtn.setimageresource (RESID);
    3. }
    4. public void Settextviewtext (String text) {
    5. Mtv.settext (text);
    6. }


Then the OnCreate () in the main interface is set by the function call.
4.2 Setting properties through XML
4.2.1 First defines the set of properties that the XML can have, creates attrs.xml under values, and the file name is arbitrary, usually called Attrs.xml

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <resources>
  3. <declare-styleable name="Imagebtnwithtext">
  4. <attr name="Android:text"/>
  5. <attr name="android:src"/>
  6. </declare-styleable>
  7. </Resources


Attribute collection name: Imagebtnwithtext, can be defined according to the actual;
The collection contains a list of properties, one property per row.
4.2.2 Modifying the custom control implementation code to get the properties defined in the XML

Java code
    1. typedarray a = context.obtainstyledattributes (attrs,  R.styleable.imagebtnwithtext);   
    2. Charsequence text = a.gettext ( R.styleable.imagebtnwithtext_android_text);   
    3. if (text != < span class= "keyword" >null)  mtv.settext (text);   
    4. drawable drawable  = a.getdrawable (R.STYLEABLE.IMAGEBTNWITHTEXT_ANDROID_SRC);   
    5. if (drawable != null)  mbtn.setimagedrawable (drawable );   
    6. a.recycle ()   


First, all the attribute arrays are obtained by context.obtainstyledattributes;
The corresponding values are then obtained through the GETXXXX () series Interfaces of the Typedarray class.
4.2.3 Setting the custom control in the main interface layout
android:text= "ABC" android:src= "@drawable/icon
4.3 Custom Name properties:
The property name used in 4.2 is the Android System namespace, starting with Android, such as Android:text.
Some property names are defined in real-world development, and these attribute names are still defined in the attrs.xml mentioned in 4.2.1:
4.3.1 defining attribute Names

XML code
    1. <attr name="AppendText" format="string"/>


Unlike attr, which uses the system directly, a format property is added to indicate what this property is in. Format options can be found in note 1
4.3.2 Using custom properties

XML code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:myspace="Http://schemas.android.com/apk/res/com.demo.customwidget"
  4. android:orientation= "vertical" android:layout_width="fill_parent "
  5. android:layout_height="fill_parent">
  6. <com.demo.widget2.ImageBtnWithText
  7. android:text= "ABC" android:src="@drawable/icon" android:id="@+id/widget "
  8. android:layout_width= "fill_parent" android:layout_gravity="center"
  9. android:layout_height= "fill_parent" myspace:appendtext="123456">
  10. </com.demo.widget2.ImageBtnWithText>
  11. </linearlayout>


Use this property directly in the main layout file appendtext= "ABC" is not set to take effect, but to define an XML namespace in the main layout xml:
Xmlns:myspace= "Http://schemas.android.com/apk/res/com.demo.customwidget"
The name of the namespace can be defined by itself, here is MySpace, that is xmlns:myspace;
The following address has a limit, which must begin with: "http://schemas.android.com/apk/res/", followed by the package name Com.demo.customwidget,
The package name here is consistent with the properties of the <manifest> node in androidmanifest.xml package= "Com.demo.customwidget", not the package where the custom control Java code resides. Of course the simple program custom control Java code is also generally within this package.

Note 1:
Note 1:format available options
"Reference"//reference
"COLOR"//Color
"Boolean"//Boolean value
"Dimension"//Dimension value
"Float"//floating-point value
"Integer"//Integer value
"String"//String
"Fraction"//percentage, e.g. 200%
Enumeration values in the following format:
<attr name= "Orientation" >
<enum name= "Horizontal" value= "0"/>
<enum name= "Vertical" value= "1"/>
</attr>
When used in XML:
Android:orientation = "vertical"

A flag bit, bit, or operation in the following format:
<attr name= "Windowsoftinputmode" >
<flag name = "Stateunspecified" value = "0"/>
<flag name = "stateunchanged" value = "1"/>
<flag name = "Statehidden" value = "2"/>
<flag name = "Statealwayshidden" value = "3"/>
<flag name = "Statevisible" value = "4"/>
<flag name = "Statealwaysvisible" value = "5"/>
<flag name = "Adjustunspecified" value = "0x00"/>
<flag name = "Adjustresize" value = "0x10"/>
<flag name = "Adjustpan" value = "0x20"/>
<flag name = "adjustnothing" value = "0x30"/>
</attr>
When used in XML:
Android:windowsoftinputmode = "Stateunspecified | stateunchanged | Statehidden ">


In addition, you can specify multiple types of values when defining a property, such as:
<attr name = "Background" format = "Reference|color"/>
When used in XML:
Android:background = "@drawable/Picture id| #00FF00"

(ext.) Android Custom Combo control

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.