On an article for everyone to achieve the most basic slide-off effect, I believe many small partners have found a small problem, modify the width of the right side of the menu, we need to modify our custom method, this is very inconvenient, the following for you to introduce how to control this change by custom attributes. The code body is the same as above, and here I'll explain how to implement a custom property.
In the Android UI design we tend to not be satisfied with the style Google provides us, then we need to customize the style, then the basic steps of custom style? 1, write the XML file, 2, in the layout file for use, pay special attention to xmlnx;3, in the construction method to get the value we set.
Here is an example of this in the following steps for everyone to explain:
XML file (Create a new attr.xml under the values file):
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "rightpadding" format= "Dimension" ></attr> <declare-styleable name= "Slidingmenu" > <attr name= "rightpadding" ></attr > </declare-styleable></resources>
Layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/AndroidXmlns:tools= "Http://schemas.android.com/tools"xmlns:hyman= "http://schemas.android.com/apk/res/Com.example.android_qq_view"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <Com.example.menu.SlidingMenu android:layout_width= "Match_parent"Android:layout_height= "Match_parent"hyman:rightpadding= "100DP"> <LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <include layout= "@layout/left_menu"/> &L TLinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/qq"/> </LinearLayout> </com.example.menu.SlidingMenu> </RelativeLayout>
The red label is where we need to change, we need to be aware of the blue part of the red section must be consistent, blue after the red part of our project package name, through Androidmanifest.xml can be found.
The custom class is constructed in a way that gets our set value:
PublicSlidingmenu (Context context) { This(Context,NULL); } /*** This method is called when no custom attribute is used *@paramContext *@paramAttrs*/ PublicSlidingmenu (Context context, AttributeSet attrs) { This(context, Attrs, 0); } /*** Called when the custom style is used *@paramContext *@paramAttrs *@paramDefstyle*/ PublicSlidingmenu (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //get the properties we defineTypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.slidingmenu, Defstyle, 0); intn = a.getindexcount ();//get the number of custom properties set for(inti=0; i<n; i++){ intattr =A.getindex (i); Switch(attr) { Caser.styleable.slidingmenu_rightpadding:mmenurightpadding= A.getdimensionpixelsize (attr, (int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, 50, Context.getresources (). Getdisplaymetrics ()); Break; }} a.recycle (); WindowManager WM=(WindowManager) context.getsystemservice (context. Window_service); Displaymetrics Outmetrics=NewDisplaymetrics (); Wm.getdefaultdisplay (). Getmetrics (Outmetrics); Mscreenwidth=Outmetrics.widthpixels; }
Code I just put three construction methods published here, the other way, you can view together.
The final effect:
QQ Left slide Display custom properties