This article is a summary of "custom controls" in the "first line of code-android" in the study of the Guo Lin great God.
Similar method, if the new method has no particular advantage, I will still use the old method. I've never understood it. Using the package name as a control in the layout file, and not knowing the benefits of this approach, so I have not used it. In the previous contact of the book on the custom control is not explained, I look at the online demo, I also do not feel the advantages of the custom control, so I have always been using the system to control. I have tried to learn custom controls from online search, but no explanation is in place. When I saw the first line of code-android, the first thing I did was to look for an introduction to "use package name as a control". I finally found it.
Custom layouts can be more complex, more consistent with what we expect, and certainly improve our programming efficiency.
Custom controls need to specify the full class name, so we'll start with a new class titlelayout inherited from LinearLayout.
public class Titlelayout (context context, AttributeSet Attrs) {public titlelayout (context context, AttributeSet Attrs) { Super (context, attrs); Layoutinflater.from (context). Inflate (R.layout.title, this);} }
In this class, we re-linearlayout a constructor with two arguments in it, and this constructor is called when the Titlelayout control is introduced into the layout.
Now that the custom control has been created, we need to add this custom control to the layout file:
<pre name= "code" class= "java" ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" > < Com.example.uicustomviews.TitleLayout android:layout_width= "match_parent" android:layout_height= "Wrap_ Content ">
</com.example.uicustomviews.TitleLayout>
</
LinearLayout
>
"Learning Notes" Android custom controls