Android custom control Custom Attributes

Source: Internet
Author: User

Custom Controls are everywhere in android, which gives us great convenience. For example, a view is a combination of multiple controls, such as imageview, imagebutton, and textview. There are many places to use. It is impossible for us to write three combinations each time, which is a waste of time, low efficiency. In this case, we can customize a view to replace them, which not only improves the efficiency but also improves the usage of xml.
1. Introduction to custom properties of Controls
The Code in the following example is defined in values/attrs. xml and attributes can be named at will.
1. reference: refer to a resource ID.
Example:
[Java] Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "background" format = "reference"/>
<Attr name = "src" format = "reference"/>
</Declare-styleable>

2. color: color value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "textColor" format = "color"/>
</Declare-styleable>

3. boolean: boolean value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "focusable" format = "boolean"/>
</Declare-styleable>

4. dimension: dimension value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "layout_width" format = "dimension"/>
</Declare-styleable>

5. float: floating point value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "fromAlpha" format = "float"/>
<Attr name = "toAlpha" format = "float"/>
</Declare-styleable>

6. integer: integer value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "frameDuration" format = "integer"/>
<Attr name = "framesCount" format = "integer"/>
</Declare-styleable>

7. string: string.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "text" format = "string"/>
</Declare-styleable>

8. fraction: percentage.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "effectx" format = "fraction"/>
<Attr name = "ty" format = "fraction"/>
</Declare-styleable>

9. enum: enumeration value.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "orientation">
<Enum name = "horizontal" value = "0"/>
<Enum name = "vertical" value = "1"/>
</Attr>
</Declare-styleable>

10. flag: bitwise OR operation.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "windowSoftInputMode">
<Flag name = "stateUnspecified" value = "0"/>
<Flag name = "stateUnchanged" value = "1"/>
<Flag name = "stateHidden" value = "2"/>
<Flag name = "stateAlwaysHidden" value = "3"/>
</Attr>
</Declare-styleable>

11. Multiple types.
Example:
[Java]Copy codeThe Code is as follows: <declare-styleable name = "name">
<Attr name = "background" format = "reference | color"/>
</Declare-styleable>

Bytes -------------------------------------------------------------------------------------------
Ii. Use of attributes and implementation of Custom Controls
1. design the components of the control and think about the custom attributes.
For example, I want to create a <shadow button with a text description at the bottom of the button> (similar to the 9-cell button)
Create values/attrs. xml
[Java]Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "custom_view">
<Attr name = "custom_id" format = "integer"/>
<Attr name = "src" format = "reference"/>
<Attr name = "background" format = "reference"/>
<Attr name = "text" format = "string"/>
<Attr name = "textColor" format = "color"/>
<Attr name = "textSize" format = "dimension"/>
</Declare-styleable>
</Resources>

The preceding parameters are defined as custom_view, custom_id as the button id, src as the button, background as the shadow background, text as the button description, textColor as the font color, and textSize as the font size.
2. How do I customize controls and use these attributes? For more information, see the code. CustomView:Copy codeThe Code is as follows: package com. nanlus. custom;
Import com. nanlus. custom. R;
Import android. content. Context;
Import android. content. res. TypedArray;
Import android. graphics. Color;
Import android. graphics. drawable. Drawable;
Import android. util. AttributeSet;
Import android. view. Gravity;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. FrameLayout;
Import android. widget. ImageButton;
Import android. widget. ImageView;
Import android. widget. TextView;
Public class CustomView extends FrameLayout implements OnClickListener {
Private CustomListener customListener = null;
Private Drawable mSrc = null, mBackground = null;
Private String mText = "";
Private int mTextColor = 0;
Private float mTextSize = 20;
Private int mCustomId = 0;
Private ImageView mBackgroundView = null;
Private ImageButton mButtonView = null;
Private TextView mTextView = null;
Private LayoutParams mParams = null;
Public CustomView (Context context ){
Super (context );
}
Public CustomView (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray a = context. obtainStyledAttributes (attrs,
R. styleable. custom_view );
MSrc = a. getDrawable (R. styleable. custom_view_src );
MBackground = a. getDrawable (R. styleable. custom_view_background );
MText = a. getString (R. styleable. custom_view_text );
MTextColor = a. getColor (R. styleable. custom_view_textColor,
Color. WHITE );
MTextSize = a. getDimension (R. styleable. custom_view_textSize, 20 );
MCustomId = a. getInt (R. styleable. custom_view_custom_id, 0 );
MTextView = new TextView (context );
MTextView. setTextSize (mTextSize );
MTextView. setTextColor (mTextColor );
MTextView. setText (mText );
MTextView. setGravity (Gravity. CENTER );
MTextView. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT,
LayoutParams. WRAP_CONTENT ));
MButtonView = new ImageButton (context );
MButtonView. setImageDrawable (mSrc );
MButtonView. setBackgroundDrawable (null );
MButtonView. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT,
LayoutParams. WRAP_CONTENT ));
MButtonView. setOnClickListener (this );
MBackgroundView = new ImageView (context );
MBackgroundView. setImageDrawable (mBackground );
MBackgroundView. setLayoutParams (new LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ));
AddView (mBackgroundView );
AddView (mButtonView );
AddView (mTextView );
This. setOnClickListener (this );
A. recycle ();
}
@ Override
Protected void onAttachedToWindow (){
Super. onAttachedToWindow ();
MParams = (LayoutParams) mButtonView. getLayoutParams ();
If (mParams! = Null ){
MParams. gravity = Gravity. CENTER_HORIZONTAL | Gravity. TOP;
MButtonView. setLayoutParams (mParams );
}
MParams = (LayoutParams) mBackgroundView. getLayoutParams ();
If (mParams! = Null ){
MParams. gravity = Gravity. CENTER_HORIZONTAL | Gravity. TOP;
MBackgroundView. setLayoutParams (mParams );
}
MParams = (LayoutParams) mTextView. getLayoutParams ();
If (mParams! = Null ){
MParams. gravity = Gravity. CENTER_HORIZONTAL | Gravity. BOTTOM;
MTextView. setLayoutParams (mParams );
}
}
Public void setCustomListener (CustomListener l ){
CustomListener = l;
}
@ Override
Public void onClick (View v ){
If (customListener! = Null ){
CustomListener. onCuscomClick (v, mCustomId );
}
}
Public interface CustomListener {
Void onCuscomClick (View v, int custom_id );
}
}

The code is very simple. Let's just talk about it. Let's take a look at how our mmview is used. Please refer:
3. Use of Custom Controls
For more information, see the code. main. xml:
[Java]Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: nanlus = "http://schemas.android.com/apk/res/com.nanlus.custom"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_centerVertical = "true"
Android: orientation = "horizontal">
<Com. nanlus. custom. CustomView
Android: id = "@ + id/custom1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Nanlus: background = "@ drawable/background"
Nanlus: custom_id = "1"
Nanlus: src = "@ drawable/style_button"
Nanlus: text = "button 1">
</Com. nanlus. custom. CustomView>
</LinearLayout>
</RelativeLayout>

Here you need to explain,
Xmlns: nanlus = "http://schemas.android.com/apk/res/com.nanlus.custom"
Nanlus is the prefix in xml, and com. nanlus. custom is the package name.
4. directly add the code in the Activity
[Java]Copy codeThe Code is as follows: package com. nanlus. custom;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ImageButton;
Import android. widget. ImageView;
Import android. widget. TextView;
Import android. widget. Toast;
Import com. nanlus. BaseActivity;
Import com. nanlus. custom. R;
Import com. nanlus. custom. CustomView. CustomListener;
Public class CustomActivity extends BaseActivity implements CustomListener {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
(CustomView) this. findViewById (R. id. custom1). setCustomListener (this );
}
@ Override
Public void onCuscomClick (View v, int custom_id ){
Switch (custom_id ){
Case 1:
Toast. makeText (this, "hello !!! ", Toast. LENGTH_LONG). show ();
Break;
Default:
Break;
}
}
}

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.