In Andriod development, a large part of the resources to deal with, such as: pictures, layout files, strings, styles and so on. This is a great difficulty for us to develop some common components, because common components may be more likely to be in the form of jar packs. However, only Java code is allowed in the Java JAR Package and no resources are available.
When we want to provide the common components we develop ourselves in the form of jar packs, we need to create resources in the form of code.
The following provides a way to create a ProgressBar using full Java code.
ProgressBar The default style is a circle, and the style we want to display as a progress bar can use the following code in the layout file:
<progressbar
android:layout_width= "fill_parent"
android:layout_height= "Wrap_content"
? Android:attr/progressbarstylehorizontal " />
This part of the code is to make ProgressBar from the style of the loop into the progress bar style. ProgressBar created in this manner cannot be included in the jar package.
We can also create ProgressBar objects in the form of pure code, as follows:
...
ProgressBar ProgressBar = new ProgressBar (context);
Lineanerlayout layout = new LinearLayout (context);
Layout.addview (ProgressBar, New Layoutparam (Layoutparam.fill_parent, layoutparam.fill_parent));
....
This creates a ProgressBar object in the form of pure code, but he's just the default style. A Non-stop circle.
At this point we may all think of it as if there is no style set. We can set the previous style, but we looked through the API and found that view did not provide any way to set the style for us.
In fact, a style is a way to set some common property values for a view or a set of view, so it is not possible to use code to set it.
We can look at what attributes are set to view in the Progressbarstylehorizontal style, and we find the values/under the framework of the RES directory Theme.xml file, search Progressbarstylehorizontal will find the following line:
<item name= "Progressbarstylehorizontal" > @android:style/widget.progressbar.horizontal</item>
The widget style for this topic is Widget.ProgressBar.Horizontal, we open the Style.xml file in the same directory, search for the style, we can find the following code:
<style name= "Widget.ProgressBar.Horizontal" >
<item name= "Android:indeterminateonly" >false</ item>
<item name= "android:progressdrawable" > @android:d rawable/progress_horizontal</item>
<item name= "android:indeterminatedrawable" > @android:d rawable/progress_indeterminate_horizontal</ item>
<item name= "android:minheight" >20dip</item>
<item name= "Android:maxheight" > 20dip</item>
</style>
That is, the progressbarstylehorizontal style is actually set the attributes as above, we set the value as above in the layout file, and the code looks like this:
<progressbar
android:layout_width= "fill_parent"
android:layout_height= "Wrap_content"
android: Indeterminateonly= "false"
android:progressdrawable= "@android:d rawable/progress_horizontal"
android: indeterminatedrawable= "@android:d rawable/progress_indeterminate_horizontal"
android:minheight= "20dip"
android:maxheight= "20dip"/>
Then run our program and find that the ProgressBar has changed from circle to progress bar style. At this point we can set these properties in the code to the values in the layout file, and the pure Java code should look like the following:
ProgressBar ProgressBar = new ProgressBar (this);
Progressbar.setindeterminate (false);
Progressbar.setprogressdrawable (Getresources (). Getdrawable (Android). r.drawable.progress_horizontal));
Progressbar.setindeterminatedrawable (Getresources (). Getdrawable (Android). r.drawable.progress_indeterminate_horizontal));
Progressbar.setminimumheight (a);
LinearLayout layout = new LinearLayout (this);
Layout.addview (ProgressBar, New Layoutparams (Layoutparams.fill_parent, layoutparams.wrap_content));
Setcontentview (layout);
When we found out that ProgressBar really turned into bars, but didn't show up as a progress bar, we carefully contrasted the differences between pure Java code and XML layout files, and we found that android:indeterminateonly= "false" and Progressbar.setindeterminate (false); The properties of the layout file have a only ending but not in the code, we look for the API discovery does not setindeterminateonly such a method.
We open the ProgressBar source code and find the. Setindeterminate (False) method with the following code:
Public synchronized void Setindeterminate (Boolean indeterminate) {
if (!monlyindeterminate | |!mindeterminate) && indeterminate!= mindeterminate) {
mindeterminate = indeterminate;
if (indeterminate) {
//swap between indeterminate and regular backgrounds mcurrentdrawable
= mindeterminatedrawable;
Startanimation ();
} else {
mcurrentdrawable = mprogressdrawable;
StopAnimation ();}}
We can now see that indeterminate and indeterminateonly are not the same thing, and we should think about it, As long as we turn the value of Indeterminateonly to false so that the ProgressBar becomes the style of the progress bar, we look up all the code and find that there is no corresponding public way to modify the value of the property.
That is, we've been discussing it for so long that it's impossible to create a progress bar style ProgressBar in pure code form.
But...
is not to change the value of a class's private variable, Java encapsulation is not as good as I think, we can completely modify the value of an object's private variable by reflection mechanism, because the article is not a reflection of the article, so here only the reflection to modify the value of the private variable code, It does not give a detailed description:
We create a new class called Beanutils.java.
The contents of the class are shown here as follows:
The public class Beanutils {private Beanutils () {}/** * Sets the object property value directly, ignoring the private/protected modifier, without the setter function. *url:http://www.bianceng.cn/os/extra/201609/50417.htm */public static void SetFieldValue (final object, fi
NAL String fieldName, final Object value {Field field = Getdeclaredfield (Object, fieldName); if (field = null) throw new IllegalArgumentException ("Could not find field [+ FieldName +"] on target [+
Object + "]");
makeaccessible (field);
try {Field.set (object, value);
catch (Illegalaccessexception e) {log.e ("ZBKC", "", e);
}/** * Cycle up to get the object's Declaredfield. */protected static Field Getdeclaredfield (Final object, final String fieldName) {return GETDECLAREDFI
Eld (Object.getclass (), fieldName);
/** * Cycle upward transformation, get the class Declaredfield. */@SuppressWarnings ("unchecked") protected static Field getDeclaredfield (Final class Clazz, final String fieldName) {for (Class superclass = clazz; superclass!= Object.cla ss Superclass = Superclass.getsuperclass ()) {try {return Superclass.getdeclaredfield (fieldName)
;
The catch (Nosuchfieldexception e) {/field is not defined in the current class, continuing upward transition}} return null;
/** * Cast Fileld accessible. */protected static void makeaccessible (Field field) {if (! Modifier.ispublic (Field.getmodifiers ()) | | !
Modifier.ispublic (Field.getdeclaringclass (). getmodifiers ())) {field.setaccessible (true); }
}
}
The tool provides a common method: public static void SetFieldValue (Final object, final String fieldName, final object value) to modify the private change of an object Value of the quantity:
At this point our ProgressBar code should look as follows:
ProgressBar ProgressBar = new ProgressBar (this);
Beanutils.setfieldvalue (ProgressBar, "mindeterminateonly", new Boolean (false));
Progressbar.setindeterminate (false);
Progressbar.setprogressdrawable (Getresources (). Getdrawable (Android). r.drawable.progress_horizontal));
Progressbar.setindeterminatedrawable (Getresources (). Getdrawable (Android). r.drawable.progress_indeterminate_horizontal));
Progressbar.setminimumheight (a);
LinearLayout layout = new LinearLayout (this);
Layout.addview (ProgressBar, New Layoutparams (Layoutparams.fill_parent, layoutparams.wrap_content));
Setcontentview (layout);
So far we've finally created a ProgressBar progress bar style using pure Java code.