This example introduces the method of custom view properties of Android for your reference. The specific content is as follows
1. Customize your own view class to inherit from View
public class MyView extends View { public MyView(Context context, AttributeSet attrs) { super(context, attrs); //Get custom attributes TypedArray ta=(attrs, ); int color=(.MyView_rect_color, 0xffff0000); setBackgroundColor(color); //It must be recycled manually (); } public MyView(Context context) { super(context); } }
2. Create a new file in the res/values directory
<?xml version="1.0" encoding="utf-8"?> <resources> //Define a declare-styleable tag and set the attr attribute inside <declare-styleable name="MyView"> <attr name="rect_color" format="color"/> </declare-styleable> </resources>
An attr attribute corresponding to a view attribute
3. Finally, see how to use the custom view we created and set its properties in the layout file
<LinearLayout xmlns:andro //Customize a MyView namespace xmlns:gu="/apk/res/" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < android:layout_width="100dp" android:layout_height="100dp" //Custom property values based on the custom namespace and the attributes we set in attrs gu:rect_color="#cc99cc" /> </LinearLayout>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.