Use Android progressbar and toast to generate an Interface

Source: Internet
Author: User

First, I need such an interface.

This interface is displayed by using audiomanager. adjuststreamvolume (INT streamtype, int direction, int flags). Remember to pass audiomanager. flag_show_ui to flags.

Otherwise, the voice of a stream is adjusted logically. the user cannot see the interface and does not know whether it is successful or not.

Then I have a functional interface that is the same as this one, but the functions are different. Well, I know that the built-in Android interface is not very nice, but here is just a description of the principles and methods. The aesthetic is not discussed.

The two core components here are textview and progressbar. If it is customized, you need to write a layout to wrap the two components and display them when you click a button.

Let's go back to the android built-in interface. When he clicks a button, it will pop up and will automatically disappear after a while. This will automatically disappear and I think it is very useful, because the user only needs to adjust it for a while.

You can see the changes on the interface. You do not need to display them all the time. Otherwise, you need to make a response at the point? How troublesome it is, simple operations are important.

So I thought that the toast provided by Android has this function. Okay! If the direction is determined, writeCodeRight

The logic is that when a user clicks a button, the interface above is displayed and disappears after a while. Here, you need to call the method to change the progress bar when displaying toast.

The Code is as follows:

 // ////////////////////////Form a toast and progress interface.
PB =NewProgressbar (context,Null,
Android. R. ATTR. progressbarstylehorizontal );
PB. setmax (gameconfig. gamespeedmax );
PB. setprogress (gameconfig. gamespeeddefault );
PB. setlayoutparams (NewLinearlayout. layoutparams (
Extensionlayoutconfig. progressbarwidth,
Linearlayout. layoutparams. wrap_content ));
Toast = toast. maketext (context,
Rfileidconvert. getstring ("Gamespeedbartitle"),
Toast. length_long );
Toast. setgravity (gravity. Top,0,0);
Linearlayout toastview = (linearlayout) toast. getview ();
Toastview. setgravity (gravity. center );
//Note that it is necessary to pass 1 here because its textview has been added during creation. If you pass 0
//The above will show the progress bar and then the text. You can do this if you need it, but now I need
//Text on the top of the progress bar below so to pass 1
Toastview. addview (Pb,1);

 

 

the following two APIs are called externally:

  /*   * 
* set the progress
*/
Public void setgamespeedbarprogress ( int progress) {
Pb. setprogress (Progress);
}
/* *
* display toast
*/
Public void gamespeedbarshow () {
toast. show ();
}

The result is as follows:

It is found that it is not so atmospheric as it is, so we still need to adjust the spacing. This is simple and I will improve it tomorrow. This is probably the function.

Summary. Please do not underestimate this interface. At the beginning, I also thought it was very easy and there were many problems that could be realized.

1In general, I am used to the Code layout, but when I encounter a progressbar, I am only speechless. In the code, the default style is circular and horizontal in the XML layout.

In XML, the default setting is <progressbar style = "? Android: ATTR/progressbarstylehorizontal"

If you want to use the code to set the style, I am sorry that there is no way to set the style. Someone will say that there is not a setscrollbarstyle (INT style) method in progressbar.

So I will talk to you after testing. This method is invalid. Please refer to the API description. Then I checked the source code to find out which setting interfaces were used for the horizontal style,

We can see which attributes are set for the view in the progressbarstylehorizontal style. We find the values/theme. xml file in the res directory under the Framework, and search progressbarstylehorizontal to find the following lines:

<Item name = "progressbarstylehorizontal"> @ Android: style/widget. progressbar. Horizontal </item>

The widget style corresponding to this topic is widget. progressbar. Horizontal. Open the style. xml file in the same directory and search for the style. You can find the following code:

<Style name = "widget. progressbar. Horizontal">

<Item name = "Android: indeterminateonly"> false </item>

<Item name = "Android: progressdrawable"> @ Android: drawable/progress_horizontal </item>

<Item name = "Android: indeterminatedrawable"> @ Android: drawable/progress_indeterminate_horizontal </item>

<Item name = "Android: minheight"> 20dip </item>

<Item name = "Android: maxheight"> 20dip </item>

</Style>

Now, you can choose not to use <item name = "progressbarstylehorizontal"> @ Android: style/widget. progressbar. Horizontal </item>

And use

Android: indeterminateonly = "false"

Android: progressdrawable = "@ Android: drawable/progress_horizontal"

Android: indeterminatedrawable = "@ Android: drawable/progress_indeterminate_horizontal"

Android: minheight = "20dip"

Android: maxheight = "20dip"/>

The horizontal progress bar can also be implemented. I know that everyone will use that line instead of so many lines. I understand. I just want to tell you how it works.

Okay, you know that I like to use code layout, and the code line is

 
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);

 

The result is that the progress bar is indeed a horizontal bar, but it is not displayed as a progress bar. Let's carefully compare the differences between pure Java code and XML layout files, and we find that

Android: indeterminateonly = "false" and progressbar. setindeterminate (false );

Not exactly. the attribute of the layout file ends with only but does not exist in the code. We found that the API does not have a method like setindeterminateonly.

Open the progressbarSource codeFind the. setindeterminate (false) method.

 

At this time, we can find that indeterminate and indeterminateonly are not the same thing,

In this case, we should think that as long as we change the value of indeterminateonly to false, the progressbar can be changed to the progress bar style,

We searched for all the code and found that no public method was provided to modify the attribute value.

 

 

That is to say, after a long discussion, we can't create a horizontal progress bar style in the form of pure code.

But... Someone said they could use reflection to change the value of a class's private variable. The content is abstract. I 'd like to skip it here, because I found that it must know the variable name before reflection succeeds,

However, I found that the variable names of some versions are different. The mindeterminateonly version is used, and the monlyindeterminate version is used for that version (Version 2.2)

In this case, I gave up the pure code layout and used

Progressbar Pb = new progressbar (context, null, Android. R. ATTR. progressbarstylehorizontal); comfortable ~~~~ This is only the first problem.

2When using toast, note that if you need the background box, you must use maketoast. If you like to clean up, you can use new toast (context) and then set some attributes and so on.

If you only want toast's short-lived display function and the interface is completely customized, we recommend using new toast (context). Finally, you need to develop the habit of viewing the source code. Sometimes you still need to do it yourself,

Good clothes and food

 

Related Article

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.