The latest version supports selector generation with drawable code, see github.com/JavaNoober/ for more details…
If there is any problem, convenient for everyone to communicate, created a QQ group, group number 887686934, welcome to join
preface
As an Android programmer, you’re familiar with shape and selector tags. Whenever the UI designer gives us a button background, we need to go to the Drawable folder and create a new bg_xxx. XML, and many times the difference is just a border color or fill color. This results in a number of very similar.xml files. Before the Internet also has a custom View, set properties in XML to achieve shape effect control. However, this custom control is not very flexible. After all, it is a custom button. If I want to change the project, I have to replace the existing Button or textView. Here’s an even easier way to do this: you don’t need to customize the View, you can just add properties to implement shapes and selectors.
The specific content
Results show
Without further ado, go straight to the code. 1. Call a line of code before the super.oncreate () of BaseActiviy, which is just a method
Layout code
Let’s add some instance properties:
Tag name +_+ attribute name of the native tag
As long as it is the View
The effect
Let’s look at this in action:
Modify the background
Now the UI designer tells us to change the background, okay, we just add or modify attributes in the XML. Let’s change the circle to a square and put a border around it. 5 seconds ok!
Simple principle analysis
app:xxx
App: XXX attributes go without saying, these are just some custom attributes. Here I’m converting some of the properties of shape and selector into custom properties that I can easily add to existing native controls.
BackgroundLibrary.inject(this)
This method is the only code that needs to be added to the framework. Inject inject adds a LayoutInflater.Factory class to LayoutInflater. An Android Activity, on the other hand, is actually created by converting XML into a View object (i.e., setContentView). Layoutinflater. Factory is a backdoor to this. It is a necessary method for XML parsing to create a View, and Google’s V7Support package uses LayoutInflater. Here, I add a custom layoutInflater. Factory to parse the added custom property in a low-intrusion way, and it’s easy. Generate the GradientDrawable, RippleDrawable, and StateListDrawable provided by the system. Since AppcompatActivity already implements layoutInflater. Factory, and Activity specifies that an Activity can only join a Factory class, this method needs to be called before super.onCreate. The AppcompatActivity factory is used to create a View. Specific principle explanation can refer to my article: Android commonly used skin and principle analysis
Specific use method:
Add a dependency:
Implementation "com. Android. Support: appcompat - v7: $supportVersion" implementation 'com. Noober. Backgorund: core: 1.0.5'Copy the code
Add code before super.onCreate of BaseActivity:
BackgroundLibrary.inject(context);
Copy the code
We support all shape attributes. The naming rule is tag name _ tag attribute name.
<attr name="shape" format="enum"> <enum name="rectangle" value="0" /> <enum name="oval" value="1" /> <enum name="line" value="2" /> <enum name="ring" value="3" /> </attr> <attr name="solid_color" format="color"/> <attr name="corners_radius" format="dimension"/> <attr name="corners_bottomLeftRadius" format="dimension"/> <attr name="corners_bottomRightRadius" format="dimension"/> <attr name="corners_topLeftRadius" format="dimension"/> <attr name="corners_topRightRadius" format="dimension"/> <attr name="gradient_angle" format="integer"/> <attr name="gradient_centerX" format="float"/> <attr name="gradient_centerY" format="float"/> <attr name="gradient_centerColor" format="color"/> <attr name="gradient_endColor" format="color"/> <attr name="gradient_startColor" format="color"/> <attr name="gradient_gradientRadius" format="dimension"/> <attr name="gradient_type" format="enum"> <enum name="linear" value="0" /> <enum name="radial" value="1" /> <enum name="sweep" value="2" /> </attr> <attr name="gradient_useLevel" format="boolean"/> <attr name="padding_left" format="dimension"/> <attr name="padding_top" format="dimension"/> <attr name="padding_right" format="dimension"/> <attr name="padding_bottom" format="dimension"/> <attr name="size_width" format="dimension"> <enum name="wrap_content" value="-2" /> <enum name="match_parent" value="-1" /> </attr> <attr name="size_height" format="dimension"> <enum name="wrap_content" value="-2" /> <enum name="match_parent" value="-1" /> </attr> <attr name="stroke_width" format="dimension"/> <attr name="stroke_color" format="color"/> <attr name="stroke_dashWidth" format="dimension"/> <attr name="stroke_dashGap" format="dimension"/> <! --> <attr name="ripple_enable" format=" Boolean "/> <attr name="ripple_color" format="color"/> <attr name="unpressed_color" format="color"/> <attr name="pressed_color" format="color"/>Copy the code
For example, add a border background like this:
<TextView
android:layout_width="130dp"
android:layout_height="36dp"
android:gravity="center"
android:text="TextView"
android:textColor="#8c6822"
android:textSize="20sp"
app:corners_radius="4dp"
app:solid_color="#E3B666"
app:stroke_color="#8c6822"
app:stroke_width="2dp" />
Copy the code
If you want a selector effect, you need to add both
app:unpressed_color
app:pressed_color
Copy the code
If water ripple effect is required, above 5.0 will be supported:
App :ripple_enable="true" app:solid_color=" XXX "// Set the default fill color app:ripple_color=" XXX "// Set the color of the water rippleCopy the code
Note: 1, if you directly add attributes to the native control, the XML will report the following red exception, you can ignore this time.
tools:ignore="MissingPrefix"
Copy the code
Can be
At the end
Project address: github.com/JavaNoober/… Subsequent updates will represent the project — your STAR ☺! If there is any problem, convenient for everyone to communicate, created a QQ group, group number 887686934, welcome to join