Android Buffet custom Control (i) custom control from layout

Source: Internet
Author: User

Android Buffet custom Control (i) custom control from layout

    • Android Buffet custom Control one from layout custom control
      • customizing controls from layout

customizing controls from layout
  1. Create a new layout in layout

    <?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent" android:layout_height="Match_parent" > <TextViewandroid:id= "@+id/tv_text"android:layout_width="Wrap_ Content "android:layout_height=" wrap_content "/>                <ImageViewandroid:id="@+id/iv_image"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:layout_below="@+id/tv_text" />                     </relativelayout>
  2. To create a new class in SRC
    The inherited class is the same as the root view of the previous step layout

     Public  class customview extends relativelayout{the view contained in the//layoutPrivateView Mrootview;PrivateTextView Mtextview;PrivateImageView Mimageview;//Overload one-parameter construction method Public CustomView(Context context) { This(Context,NULL);}//Overload two-parameter construction method Public CustomView(context context, AttributeSet attrs) {Super(context, attrs); Mrootview=layoutinflater.from (context). Inflate (R.layout.layout_view, This,true);    mtextview= (TextView) Mrootview.findviewbyid (R.id.tv_text); mimageview= (ImageView) Mrootview.findviewbyid (r.id.iv_image);}//Custom method Public void SetText(String text) {Mtextview.settext (text);}//Custom method Public void Setimageresource(intResId) {Mimageview.setimageresource (resId);}}
  3. It is now possible to use it as a normal control.

Android Buffet custom Control (i) custom control from layout

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.