To create a ProgressBar object using code

Source: Internet
Author: User
Tags modifier reflection
Android Sets the Style property in Java code--Creates a ProgressBar object with code

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"

style= "Android:attr/progressbarstylehorizontal"/>

The key code above is the red part of the code that makes the ProgressBar from the style of the circle to the progress bar. 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 (20);

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:


public class Beanutils {

Private Beanutils () {

}

/**

* Set the object property value directly, ignoring the private/protected modifier without the setter function.

*/

public static void SetFieldValue (Final object, final 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 the transition to get the object's Declaredfield.

*/

protected static Field Getdeclaredfield (Final object, final String fieldName) {

Return Getdeclaredfield (Object.getclass (), fieldName);

}

/**

* Cycle upward transformation, get the Declaredfield of the class.

*/

@SuppressWarnings ("Unchecked")

protected static Field Getdeclaredfield (final Class clazz, final String fieldName) {

for (Class superclass = clazz; superclass!= object.class; superclass = Superclass.getsuperclass ()) {

try {

Return Superclass.getdeclaredfield (FieldName);

catch (Nosuchfieldexception e) {

field is not defined in the current class, continue to transition upward

}

}

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, "Monlyindeterminate", 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 (20);

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.

======================================================================

SOURCE http://download.csdn.net/detail/codehxy/7497851

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.