Android Custom Control custom properties detailed introduction to _android

Source: Internet
Author: User
Tags xmlns
Custom controls are ubiquitous in Android, and custom controls give us a lot of convenience. For example, a view for ImageView, ImageButton, TextView and many other control combinations, with a lot of places, we can not write each time 3 of the combination, both waste time, efficiency and low. In this case, we can customize a view to replace them, not only improve the efficiency and the use of XML is quite beautiful.
Introduction to the custom properties of a control
In the following example, the code is defined in Values/attrs.xml and the properties are optionally named.
1. Reference: Refer to a resource ID.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name = "Name" >
<attr name = "Background" format = "Reference"/>
<attr name = "src" format = "Reference"/>
</declare-styleable>

2. Color: Colour value.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name = "Name" >
<attr name = "TextColor" format = "Color"/>
</declare-styleable>

3. Boolean: Boolean value.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name = "Name" >
<attr name = "focusable" format = "Boolean"/>
</declare-styleable>

4. Dimension: Dimension value.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name = "Name" >
<attr name = "layout_width" format = "Dimension"/>
</declare-styleable>

5. Float: floating-point value.
Example:
[Java]
Copy Code code 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 Code code as follows:

<declare-styleable name = "Name" >
<attr name = "frameduration" format= "integer"/>
<attr name = "Framescount" format= "integer"/>
</declare-styleable>

7. String: Strings.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name = "Name" >
<attr name = "text" format = "string"/>
</declare-styleable>

8. Fraction: percentage.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name= "Name" >
<attr name = "Pivotx" format = "fraction"/>
<attr name = "Pivoty" format = "fraction"/>
</declare-styleable>

9. Enum: enumeration value.
Example:
[Java]
Copy Code code as follows:

<declare-styleable name= "Name" >
<attr name= "Orientation" >
<enum name= "Horizontal" value= "0"/>
<enum name= "Vertical" value= "1"/>
</attr>
</declare-styleable>

Flag: Bit or operation.
Example:
[Java]
Copy Code code 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 Code code as follows:

<declare-styleable name = "Name" >
<attr name = "Background" format = "Reference|color"/>
</declare-styleable>

-------------------------------------------------------------------------------------------
second, the use of properties and the implementation of custom controls
1, think of the components of the control, consider the required custom attributes.
For example: I want to make a < shaded button, the button is just below the text description > (similar to the 9 Sudoku button)
New Values/attrs.xml
[Java]
Copy Code code 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>

Above, defined as custom_view,custom_id for button id,src as button, background for shadow background, text for button description, TextColor for font color, textsize for font size.
2, how to customize the control, how to use these properties? If you don't say much, please look at the code, CustomView:
Copy Code code 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 {
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, do not say more, the following to see how our CustomView is used, please see:
3, the use of custom controls
Don't say much, please look at the code, Main.xml:
[Java]
Copy Code code 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, Com.nanlus.custom is the package name
4, in the activity, directly on the code
[Java]
Copy Code code 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 oncr Eate (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.