Custom Controls--composite controls (TitleBar)

Source: Internet
Author: User

custom control knowledge from the Guo Lin and other great God learning, here only add their own understanding and practice, not plagiarism.  A composite control is the simplest way to customize a control, but it is a step-by-step procedure for getting started with custom controls:So what are the common combinations of controls? For example, the use of reusable interface layouts that are common in titlebar and views can be customized by combining controls, and by exposing methods and callbacks to other classes to display, hide, picture, animate, and control text content. So much nonsense, write a combo control to deepen the impact. First of all, the titlebar should contain those contents, the right and left three groups of controls, respectively, the buttons and the middle of the title bar, but some of the page is not realistic right button, or other controls do not display, then there will be the view component of the hidden and display problems. So what's the problem, by the color of the title or the color of the left and right button font and the content of the text. That's a lot of crap, you write a component let's see. Okay, the code goes: XML layout file:
  1. <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/rl_top"Android:layout_width= "Fill_parent"Android:layout_height= "@dimen/top_height"Android:background= "#f8f8f8" >    <ButtonAndroid:id= "@+id/btn_top_back"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:layout_centervertical= "true"android:gravity= "Center"Android:background= "@drawable/btn_top_back"android:textsize= "20SP" />    <TextViewAndroid:id= "@+id/tv_top_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"android:gravity= "Center"Android:text= "@string/app_name"Android:textcolor= "@color/black"android:textsize= "20SP" />    <ButtonAndroid:id= "@+id/btn_top_right"Android:layout_width= "Wrap_content"Android:layout_height= "Fill_parent"Android:layout_alignparentright= "true"android:layout_centervertical= "true"Android:layout_marginright= "@dimen/image_corner_radius"Android:background= "@null"android:gravity= "Center"Android:textcolor= "@drawable/btn_top_right_selector"android:textsize= "16SP" />    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_alignparentbottom= "true"Android:layout_height= "1DP"Android:background= "@color/gray" /></Relativelayout>


Composition control: Loading layout files and initializing views within the constructor method, by writing some common methods to control component display and hiding, and content to controlTitleviewthe above content.
  1.  Public classTitleviewextendsFramelayout {PrivateButton Btn_top_back; PrivateTextView Tv_top_title; PrivateButton btn_top_right;  PublicTitleview (Context context, AttributeSet attrs) {Super(context, attrs); Layoutinflater.from (context). Inflate (R.layout.common_top, This); Btn_top_back=(Button) Findviewbyid (r.id.btn_top_back); Tv_top_title=(TextView) Findviewbyid (r.id.tv_top_title); Btn_top_right=(Button) Findviewbyid (r.id.btn_top_right); }     Public voidSetVisible (intLeftintMiddle,intRight )        {btn_top_back.setvisibility (left);        Tv_top_title.setvisibility (middle);    Btn_top_right.setvisibility (right); }     Public voidSettitle (String title) {Tv_top_title.settext (title); }     Public voidSetleftonclicklistener (Onclicklistener onclicklistener) {btn_top_back.setonclicklistener (OnClickListener); }     Public voidSetrightonclicklistener (Onclicklistener onclicklistener) {btn_top_right.setonclicklistener (OnClickListener); }         Public voidSetrighttext (String text) {btn_top_right.settext (text); }}

    In the layout, you can reuse the title layout file through the package name + class name, initialize it in activity or fragement, control the display and hiding of the content, and the response of the control event through the Titleview method.

Custom Controls--composite controls (TitleBar)

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.