Customizing a view in Android is a detailed approach _android

Source: Internet
Author: User
Tags sqlite database

The examples in this article describe a way to customize a view in Android. Share to everyone for your reference, specific as follows:

Android Custom View implementation is relatively simple, nothing more than inheriting the parent class, and then overloaded methods, even so, in the actual coding will inevitably encounter some pits, I have encountered some of my problems and solutions summed up, I hope that the vast number of friends to help.

Note ① when defining layout with XML, Root element is best to use the merge

When we need to inherit a more complex viewgroup (more of LinearLayout, relativelayout), we usually write the layout with XML, and then inflate the XML file that defines layout in the custom view class.

First, create a new class file named Mylayout that resolves the xml file that is later defined in the Init method.

/**
 * Created by Liangfei on 4/14/15.
 * * Public
class Mylayout extends LinearLayout {public
  mylayout (context) {
    super (context);
    Init ();
  }
  private void Init () {
    setorientation (VERTICAL);
    View Rootview = Inflate (GetContext (), r.layout.my_layout, this);
    ((TextView) Rootview.findviewbyid (R.id.title)). SetText ("Mylayout");
    ((TextView) Rootview.findviewbyid (R.id.desc)). SetText ("A Customized Layout");
  }


A new layout file named My_layout is created and the Root element is set to merge.

<?xml version= "1.0" encoding= "Utf-8"?> <merge xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <textview
    android:id=" @+id/title "
    android:textsize=" 16sp "
    android:layout_width = "Wrap_content"
    android:layout_height= "wrap_content"/>
  <textview android:id=
    "@+id/desc"
    Android:layout_width= "Wrap_content"
    android:layout_height= "wrap_content"/>
</merge>

Use the Monitor tools included with the Android SDK to view the layout information for the runtime.

The top floor is a framelayout, then a linearlayout, with two textview inside, and you can see that the layout is not redundant.

But what happens if you change the Root element to LinearLayout?

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" wrap_content "
  android:layout_" height= "Wrap_content" >
  <textview
    android:id= "@+id/title"
    android:textsize= "16sp"
    Android : layout_width= "wrap_content" android:layout_height= "wrap_content"/> <textview android:id=
    " @+id/desc "
    android:layout_width=" wrap_content "
    android:layout_height=" wrap_content "/>
</ Linearlayout>

Obviously, after using LinearLayout as Root element, the layout is one more level, which is a factor affecting performance.

Note Dot ② The overloaded subclass constructor to figure out what the parent class did

Start with a bitter lesson from me, when I did this by customizing a button:

/**
 * Created by Liangfei on 4/14/15.
 */Public
class MyButton extends Button {public
  MyButton (context
    , null);
  Public MyButton (context, AttributeSet attrs) {This
    (context, attrs, 0);
  }
  Public MyButton (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr);
    Init ();
  }
}

At first glance it looks like there's no problem, the constructor is invoked correctly, but no matter how I modify the MyButton properties, the display is incorrect.

In fact, the problem is that the button class uses a defstyleattr in the constructor, and my writing ignores this defstyleattr- Com.android.internal.r.attr.buttonstyle, slightly read the source code will know.

@RemoteView Public
class button extends TextView {
    the context, NULL, the public button.
  "Public Button" (context,
  AttributeSet attrs) {The context
    , Attrs, Com.android.internal.r.attr.buttonstyle);
  }
  Public Button (context, AttributeSet attrs, int defstyleattr) {This
    (context, Attrs, defstyleattr, 0);
  } Public
  Button (context, AttributeSet attrs, int defstyleattr, int defstyleres) {
    Super (context, attrs, Defstyleattr, defstyleres);
  }


Later, when I wrote the code, I looked at the source of the parent class and then dared to write, if not sure, honestly wrote the following form.

/**
 * Created by Liangfei on 4/14/15.
 * * Public
class MyButton extends Button {public
  MyButton (context) {
    super);
    Init ();
  }
  Public MyButton (context, AttributeSet attrs) {
    Super (context, attrs);
    Init ();
  }
  Public MyButton (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr);
    Init ();
  }
}

In fact, there are many other pits, such as the height of the Button, after the time to sum up

More interested readers of Android-related content can view the site's topics: "The Overview of Android View View Tips", "Android operation XML Data Skills Summary", "Android programming activity Operation Skills Summary", " Android Resource Operation tips Summary, "Android file Operation Tips", "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operating skills summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.