Android Custom View (vii)------custom controls combining faux Actionbar controls

Source: Internet
Author: User

We've written a sample of 6 custom view in front of us, all new self-drawn controls. In this example, let's combine several existing controls into a new control.

We use two buttons and a textview combination to become a actionbar, the following first one:


Key code: (1) res/layout/custom_action_bar.xml----Combo Control layout file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "> < Button android:id= "@+id/button_ok" android:layout_width= "wrap_content" android:layout_height= "Wrap_co         Ntent "android:background=" @drawable/button_ok_click "android:layout_alignparentleft=" true "/> <TextView        Android:id= "@+id/title" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centerinparent= "true" android:visibility= "Gone"/> <button android:id= "@+ Id/button_no "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:backgr ound= "@drawable/button_no_click" android:layout_alignparentright= "true"/></relativelayout>

(2) Customactionbar.java------Custom Combo Controls

Import Android.content.context;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import Android.widget.relativelayout;import Android.widget.textview;public class Customactionbar extends relativelayout{private button button_ok;private button Button_no;private TextView title;private actionbarclickedlistener actionbarclickedlistener;public CustomActionbar (    Context context) {This (context, NULL);    } public Customactionbar (context context, AttributeSet Attrs) {This (context, attrs, 0);        } public Customactionbar (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    Layoutinflater.from (context). Inflate (R.layout.custom_action_bar, this, true);        } protected void Onfinishinflate () {super.onfinishinflate ();        BUTTON_OK = (Button) Findviewbyid (R.ID.BUTTON_OK); Button_ok.setonclicklistener (New Onclicklistener () {@Overridepublic voidOnClick (view view) {//TODO auto-generated Method stub if (Actionbarclickedlistener! = null) {Actionbarclickedlistener.                Onactionbarokclicked ();        }}});        Button_no = (Button) Findviewbyid (r.id.button_no); Button_no.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (view view) {//TODO auto-generated                Method stub if (Actionbarclickedlistener! = null) {actionbarclickedlistener.onactionbarcancelclicked ();                }}});    title = (TextView) Findviewbyid (r.id.title);    } public void Settitle (int resId) {title.settext (resId);    Title.setvisibility (view.visible); } public void Setactionbarclickedlistener (Actionbarclickedlistener actionbarclickedlistener) {this.actionbarClicked    Listener = Actionbarclickedlistener;        } public interface Actionbarclickedlistener {public void onactionbarokclicked ();    public void onactionbarcancelclicked (); }}

(3) res/layout/activity_main.xml-------Master File layout file

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >

<com.example.customviewactionbar.customactionbar
Android:id= "@+id/customactionbar"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>
</RelativeLayout>


(4) Mainactivity.java-----Main File

Import Com.example.customviewactionbar.customactionbar.actionbarclickedlistener;import Android.os.Bundle;import Android.app.activity;import Android.util.log;public class Mainactivity extends Activity implements Actionbarclickedlistener{public static final String TAG = "Customactionbar";p rivate customactionbar customactionbar;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); init ();} private void Init () {//TODO auto-generated method Stubcustomactionbar = (Customactionbar) Findviewbyid ( R.id.customactionbar); Customactionbar.setactionbarclickedlistener (this);//customactionbar.settitle ( Getresources (). GetText (R.string.hello_world)); Customactionbar.settitle (R.string.hello_world);} @Overridepublic void onactionbarokclicked () {//TODO auto-generated method stublog.i (TAG, "CLICK_OK");} @Overridepublic void onactionbarcancelclicked () {//TODO auto-generated method stublog.i (TAG, "Click_cancel"); Finish () ;}}

Custom view summary for a composite control:

1. Customize the combo control to get the control you want

2. Custom control, import layout file, implement self-desired control

3. Use in other layout files


SOURCE Download:

http://download.csdn.net/detail/hfreeman2008/8421039


Resources:

1.Android software development for custom controls

http://blog.csdn.net/jackhenry/article/details/7340474

2.android-Customize (Combine) controls + custom control skins

Http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html

Android Custom View (vii)------custom controls combining faux Actionbar controls

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.