基本视图——ProgressBar

作者:追风剑情 发布于:2015-8-25 22:09 分类:Android

示例一

视图


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <ProgressBar android:id="@+id/progressbar"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. </LinearLayout>


代码


  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.widget.ProgressBar;
  8.  
  9. public class BasicViews2Activity extends Activity {
  10.  
  11. static int progress;
  12. ProgressBar progressBar;
  13. int progressStatus = 0;
  14. Handler handler = new Handler();
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_basic_views2);
  19. progress = 0;
  20. progressBar = (ProgressBar) findViewById(R.id.progressbar);
  21. new Thread(new Runnable(){
  22. public void run(){
  23. while (progressStatus < 10){
  24. progressStatus = doSomeWork();
  25. }
  26. handler.post(new Runnable(){
  27. public void run(){
  28. //View.INVISIBLE : 隐藏(仍旧在活动中占据空间)
  29. //View.GONE : 从活动中移除,它不再占据任何空间。
  30. progressBar.setVisibility(View.GONE);
  31. }
  32. });
  33. }
  34. }).start();
  35. }
  36. private int doSomeWork(){
  37. try{
  38. Thread.sleep(500);
  39. } catch (InterruptedException e){
  40. e.printStackTrace();
  41. }
  42. return ++progress;
  43. }
  44. }


运行效果

p1.png

示例二

视图


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <ProgressBar android:id="@+id/progressbar"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. style="@android:style/Widget.ProgressBar.Horizontal" />
  12. </LinearLayout>


代码


  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.widget.ProgressBar;
  8.  
  9. public class BasicViews2Activity extends Activity {
  10.  
  11. static int progress;
  12. ProgressBar progressBar;
  13. int progressStatus = 0;
  14. Handler handler = new Handler();
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_basic_views2);
  19. progress = 0;
  20. progressBar = (ProgressBar) findViewById(R.id.progressbar);
  21. progressBar.setMax(200);
  22. new Thread(new Runnable(){
  23. public void run(){
  24. while (progressStatus < 200){
  25. progressStatus = doSomeWork();
  26. handler.post(new Runnable(){
  27. public void run(){
  28. progressBar.setProgress(progressStatus);
  29. }
  30. });
  31. }
  32. handler.post(new Runnable(){
  33. public void run(){
  34. //View.VISIBLE : 显示
  35. //View.INVISIBLE : 隐藏(仍旧在活动中占据空间)
  36. //View.GONE : 从活动中移除,它不再占据任何空间。
  37. progressBar.setVisibility(View.GONE);
  38. }
  39. });
  40. }
  41. }).start();
  42. }
  43. private int doSomeWork(){
  44. try{
  45. Thread.sleep(500);
  46. } catch (InterruptedException e){
  47. e.printStackTrace();
  48. }
  49. return ++progress;
  50. }
  51. }


运行效果

p2.png


ProgressBar样式还可以使用以下常量:

Widget.ProgressBar.Horizontal

Widget.ProgressBar.Small

Widget.ProgressBar.Large

Widget.ProgressBar.Inverse

Widget.ProgressBar.Small.Inverse

Widget.ProgressBar.Large.Inverse

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号