Custom view can probably be divided into:
- Inherit view
- Combination View
- own picture (with paint and canvas)
- Custom ViewGroup (widget)
Recently chatted with schoolmate, then prepares oneself realizes a ImageButton to practice practicing. (simplest) and a subsequent case of combining ImageView with TextView.
ImageButton
the easiest way to realize ImageButton is to put the button and ImageView together. Well, it's easy for people to think that this is a combination of custom view. But how to put an image on the button, it is natural to use a framelayout button and ImageView wrapped up, and then the layout as a weidgt. I started to understand a layout as a control. Then I learned something.
Supplementary knowledge points (Android's top-level view)
in Android, the active view of your app interface is not the most top-level view. We should all know viewtree. On the Andri side, the top view is something called Decorview. It includes your status bar, title bar, and your active interface. And this active interface, Android will automatically help you get a framelayout in advance. It can be understood that, in the presence of Andrew the God, your activity is in the view of his framelayout. Last Picture ~
Decorview->linearlayout (status bar and activity)->framelayout (the activity)-> your own acitivty view.
Example explanation
all right, back to the point. I'm on the code.
Myimagebutton_layout.xml
<framelayout
xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match _parent "
android:layout_height=" match_parent ">
<button
android:layout_width=" Match_parent "
android:layout_height= "match_parent"
android:id= "@+id/button_imagebutton" />
<imageview
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:id= "@+id/" Imageview_button " />
</FrameLayout>
Use a framelayout to load two controls, and then combine them. After we've assembled, we're going to customize the properties of our control. button does not change the right, with the original is good, that is, the need to have a picture of SRC this property is good.
Attrs.xml files in the values directory _
<?xml version= "1.0" encoding= "Utf-8"?>
<resources> in
<declare-styleable name= " Myimagebutton ">
<attr name=" imagesrc "format=" reference "/>
</declare-styleable>
</resources>
Generally, the name of the declare-styleable tag is the name of your custom control. attr is your custom control property. Format is the data type of the value of this property. The reference here represents a reference. Dimension represents the size of a DP or SP. There are some you can go and see for yourself. Defining the attributes, we start writing the controls.
Myimagebutton.java
public class MyImageButton2 extends framelayout{ImageView;
Button Mbutton;
int resid;
Public MyImageButton2 {Super (context);
Public MyImageButton2 (context, AttributeSet attrs) {Super (context, attrs);
Import layout layoutinflater.from. Inflate (r.layout.myimagebutton_layout, this);
mimageview= (ImageView) Findviewbyid (R.id.imageview_button);
Mbutton = (Button) Findviewbyid (R.id.button_imagebutton);
Gets the properties for this control.
TypedArray a = GetContext (). Obtainstyledattributes (Attrs, R.styleable.myimagebutton);
try{//Get attribute value Resid = A.getresourceid (r.styleable.myimagebutton_imagesrc, 0);
}finally {//Recycle this object a.recycle ();
} if (resid!= 0) {mimageview.setimageresource (RESID);
} public void setimage (int resid) {mimageview.setimageresource (RESID); }
}
In general, the inheritance view (so to speak, is all inherited view anyway), the implementation of two or three constructors can be. In the second there is a attr, which is the attribute that is passed in. If Resid is not the default, it means that the user has passed the attribute value in XML, and you must also write a set method that allows the user to change the attribute value in Java code.
Test.myimagebutton.layout.xml_
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
< Com.example.think.testview.customview.MyImageButton2
xmlns:pt = "http://schemas.android.com/apk/res/ Com.example.think.testview "
android:layout_height=" 100DP "
android:layout_width=" match_parent "
pt:i MAGESRC = "@drawable/ic_launcher"
/>
</LinearLayout>
Effect chart
Another instance
Here we look at an example of a view combination control:
<string-array name= "Bao_type" > <item> test 1</item> <item> Test 2 </item> <item> test 3</item> <item> test 4</item> </string-array>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @color/gray _common_background "android:orientation=" vertical "> <linearlayout android:layout_width=" Match_paren T "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <textview android:
Id= "@+id/txt_viewpage_bar1" style= "@style/style_common_pagebar" android:textcolor= "@color/theme_red"/> <textview android:id= "@+id/txt_viewpage_bar2" style= "@style/style_common_pagebar"/> <
TextView android:id= "@+id/txt_viewpage_bar3" style= "@style/style_common_pagebar"/> <textview Android:id= "@+id/txt_viewpage_bar4" style= "@style/style_common_pagebar"/> </LinearLayout> & Lt LinearLayout Android:layOut_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <Vi
EW android:id= "@+id/view_viewpage_bar1" style= "@style/style_common_pagebar_view"/> <view Android:id= "@+id/view_viewpage_bar2" style= "@style/style_common_pagebar_view"/> <view androi D:id= "@+id/view_viewpage_bar3" style= "@style/style_common_pagebar_view"/> <view android:id= "@+ ID/VIEW_VIEWPAGE_BAR4 "style=" @style/style_common_pagebar_view "/> </LinearLayout> <android . Support.v4.view.ViewPager android:id= "@+id/common_viewpage_buttom" android:layout_width= "Match_parent" and
roid:layout_height= "0DP" android:layout_weight= "1"/> </LinearLayout>
public interface Slbpageviewonpagechangelistener {void onpageselected (int position)
;
void onpagescrollstatechanged (int state); }
public class Slbpageviewbar extends LinearLayout {private textview[] mtotaltxtviews;
Private view[] mtotalviews;
Private list<textview> mtxtviews;
Private list<view> mviews;
Private Viewpager Mviewpager;
Private Slbpageviewonpagechangelistener Mslbpageviewonpagechangelistener;
Public Slbpageviewbar (context, AttributeSet attrs) {Super (context, attrs);
Mtotaltxtviews = new Textview[4];
Mtotalviews = new View[4];
Mtxtviews = new arraylist<> ();
Mviews = new arraylist<> ();
Initview ();
} private void Initview () {view.inflate (GetContext (), R.layout.layout_common_top_pageview_bar, this);
Mviewpager = (Viewpager) Findviewbyid (R.id.common_viewpage_buttom);
Mtotaltxtviews[0] = (TextView) Findviewbyid (R.ID.TXT_VIEWPAGE_BAR1);
MTOTALTXTVIEWS[1] = (TextView) Findviewbyid (R.ID.TXT_VIEWPAGE_BAR2);
MTOTALTXTVIEWS[2] = (TextView) Findviewbyid (R.ID.TXT_VIEWPAGE_BAR3); MtotalTXTVIEWS[3] = (TextView) Findviewbyid (R.ID.TXT_VIEWPAGE_BAR4);
Mtotalviews[0] = Findviewbyid (R.ID.VIEW_VIEWPAGE_BAR1);
MTOTALVIEWS[1] = Findviewbyid (R.ID.VIEW_VIEWPAGE_BAR2);
MTOTALVIEWS[2] = Findviewbyid (R.ID.VIEW_VIEWPAGE_BAR3);
MTOTALVIEWS[3] = Findviewbyid (R.ID.VIEW_VIEWPAGE_BAR4); /** * @param title title name array/public void Settitleview (string[] title) {int legth = Title.length > 4?
4:title.length;
for (int i = 0; i < legth i++) {mtotaltxtviews[i].settext (title[i]);
Mtotaltxtviews[i].setvisibility (view.visible);
Mtotalviews[i].setvisibility (view.invisible);
Mtotaltxtviews[i].setonclicklistener (New Slbtextviewonlister (i));
Mtxtviews.add (Mtotaltxtviews[i]);
Mviews.add (Mtotalviews[i]);
} mtotalviews[0].setvisibility (view.visible);
Mviewpager.setonpagechangelistener (Monbuttompagechangelistener);
Public Viewpager Getviewpager () {return mviewpager; }
public void Setslbpageviewonpagechangelistener (Slbpageviewonpagechangelistener slbpageviewonpagechangelistener) {
This.mslbpageviewonpagechangelistener = Slbpageviewonpagechangelistener; } private void Changeviewbg (int textviewid, int viewid) {for (View view:mviews) {if (Viewid = = VIEW.G
Etid ()) {view.setvisibility (view.visible);
else {view.setvisibility (view.invisible); } for (TextView view:mtxtviews) {if (Textviewid = = View.getid ()) {View.settextcolor (getres
Ources (). GetColor (r.color.theme_red));
else {View.settextcolor (Getresources (). GetColor (r.color.black_normal_text_33333));
Class Slbtextviewonlister implements Onclicklistener {private int mitem;
Public slbtextviewonlister (int item) {This.mitem = Item;
@Override public void OnClick (View v) {mviewpager.setcurrentitem (Mitem);
} Private Viewpager.onpagechangelistener Monbuttompagechangelistener = new Viewpager.onpagechangelistener () {
@Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {} @Override public void onpageselected (int position) {switch (position) {case 0:changevie
WBg (R.id.txt_viewpage_bar1, R.ID.VIEW_VIEWPAGE_BAR1);
Break
Case 1:CHANGEVIEWBG (R.ID.TXT_VIEWPAGE_BAR2, R.ID.VIEW_VIEWPAGE_BAR2);
Break
Case 2:CHANGEVIEWBG (R.ID.TXT_VIEWPAGE_BAR3, R.ID.VIEW_VIEWPAGE_BAR3);
Break
Case 3:CHANGEVIEWBG (R.ID.TXT_VIEWPAGE_BAR4, R.ID.VIEW_VIEWPAGE_BAR4);
Break
} if (Mslbpageviewonpagechangelistener!= null) mslbpageviewonpagechangelistener.onpageselected (position); @Override public void onpagescrollstatechanged (int state) {if MslbpageviewoNpagechangelistener!= null) mslbpageviewonpagechangelistener.onpagescrollstatechanged (state);
}
};
}
public class Commonviewpageadapter extends Fragmentpageradapter {private List<fra
Gment> mfragments;
Private String [] mtitles;
Public Commonviewpageadapter (fragmentmanager FM, list<fragment> fragments, String [] titles) {super (FM);
Mfragments = fragments;
Mtitles = titles;
@Override public Fragment getitem (int position) {return mfragments.get (position);
@Override public int GetCount () {return mfragments.size ();
@Override public charsequence getpagetitle (int position) {return mtitles[position]; @Override public void Destroyitem (ViewGroup container, int position, object) {Super.destroyitem (con
Tainer, Position, object); }
}
<com.slfinance.app.slb.common.ui.view.slbpageviewbar android:id=" @+id/slbpv_buy _huoqibao_slbpageviewbarshow "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Andr" oid:orientation= "vertical"/>
private void Initbuttompagefragment (Slbpageviewbar slbpageviewbar) {
list<fragment> fragmentlist = new Arraylist<> ();
Test1fragment test1fragment = new Test1fragment ();
Test2fragment test2fragment = new Test2fragment ();
Test3fragment test3fragment = new Test3fragment ();
Test4fragment test4fragment = new Test4fragment ();
Fragmentlist.add (test1fragment);
Fragmentlist.add (test2fragment);
Fragmentlist.add (test3fragment);
Fragmentlist.add (test4fragment);
String[] titles = Getresources (). Getstringarray (r.array.buy_huoqi_bao_zanquan_detail);
Slbpageviewbar.settitleview (titles);
Commonviewpageadapter viewpageradapter = new Commonviewpageadapter (
Getsupportfragmentmanager (), FragmentList, titles);
Mbuttompageview.setadapter (viewpageradapter);
Mbuttompageview.setoffscreenpagelimit (3);
Mbuttompageview.setcurrentitem (0);
}