Android控制項之ProgressBar

來源:互聯網
上載者:User


1,帶有進度條的ProgressBar
[html] 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
 
    // Request the progress bar to be shown in the title 
    requestWindowFeature(Window.FEATURE_PROGRESS); 
    setContentView(R.layout.progressbar_1); 
    setProgressBarVisibility(true);//設定在title裡的ProgressBar可見 
     
    final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal); 
     
    setProgress(progressHorizontal.getProgress() * 100);//為title中的ProgressBar設定進度 
    setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);//為title中的ProgressBar設定二級進度 
     
    Button button = (Button) findViewById(R.id.increase);//一級進度遞增 
    button.setOnClickListener(new Button.OnClickListener() { 
        public void onClick(View v) { 
            progressHorizontal.incrementProgressBy(1); 
            // Title progress is in range 0..10000 
            setProgress(100 * progressHorizontal.getProgress());//為title中的ProgressBar設定進度 
        } 
    }); 
 
    button = (Button) findViewById(R.id.decrease);//一級進度遞減 
    button.setOnClickListener(new Button.OnClickListener() { 
        public void onClick(View v) { 
            progressHorizontal.incrementProgressBy(-1); 
            // Title progress is in range 0..10000 
            setProgress(100 * progressHorizontal.getProgress());//為title中的ProgressBar設定進度 
        } 
    }); 
 
    button = (Button) findViewById(R.id.increase_secondary);//二級進度遞增 
    button.setOnClickListener(new Button.OnClickListener() { 
        public void onClick(View v) { 
            progressHorizontal.incrementSecondaryProgressBy(1); 
            // Title progress is in range 0..10000 
            setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress()); 
        } 
    }); 
 
    button = (Button) findViewById(R.id.decrease_secondary);//二級進度遞減 
    button.setOnClickListener(new Button.OnClickListener() { 
        public void onClick(View v) { 
            progressHorizontal.incrementSecondaryProgressBy(-1); 
            // Title progress is in range 0..10000 
            setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress()); 
        } 
    }); 
     

設定檔 :
[html] 
<ProgressBar android:id="@+id/progress_horizontal" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="200dip" 
    android:layout_height="wrap_content" 
    android:max="100" 
    android:progress="50" 
    android:secondaryProgress="75" /> 

 

:

 

2, 轉圈的樣式的ProgressBar
[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
 
    <ProgressBar 
        android:id="@+android:id/progress_large" 
        style="?android:attr/progressBarStyleLarge"//大樣式 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
 
    <ProgressBar                                  //預設 
        android:id="@+android:id/progress" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
 
    <ProgressBar 
        android:id="@+android:id/progress_small"   //小樣式 
        style="?android:attr/progressBarStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
 
    <ProgressBar                                   //小標題樣式 
        android:id="@+android:id/progress_small_title" 
        style="?android:attr/progressBarStyleSmallTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
 
</LinearLayout> 

Java代碼:
[html] 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
 
    // Request for the progress bar to be shown in the title 
    requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS); 
     
    setContentView(R.layout.progressbar_2); 
     
    // Make sure the progress bar is visible 
    setProgressBarVisibility(true); 

效果:


3,Dialog樣式的ProgressBar
[html] 
private static final int DIALOG1_KEY = 0; 
   private static final int DIALOG2_KEY = 1; 
 
 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
 
       setContentView(R.layout.progressbar_3); 
 
       Button button = (Button) findViewById(R.id.showIndeterminate); 
       button.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View v) { 
               showDialog(DIALOG1_KEY); 
           } 
       }); 
 
       button = (Button) findViewById(R.id.showIndeterminateNoTitle); 
       button.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View v) { 
               showDialog(DIALOG2_KEY); 
           } 
       }); 
   } 
 
   @Override 
   protected Dialog onCreateDialog(int id) { 
       switch (id) { 
           case DIALOG1_KEY: { 
               ProgressDialog dialog = new ProgressDialog(this); 
               dialog.setTitle("Indeterminate"); 
               dialog.setMessage("Please wait while loading..."); 
               dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//設定轉圈的效果 
               dialog.setCancelable(true); 
               return dialog; 
           } 
           case DIALOG2_KEY: { 
               ProgressDialog dialog = new ProgressDialog(this); 
               dialog.setMessage("Please wait while loading..."); 
               dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//設定轉圈的效果 
               dialog.setCancelable(true); 
               return dialog; 
           } 
       } 
       return null; 
   } 

效果:


4,在title上面的轉圈的ProgressBar
[java] 
private boolean mToggleIndeterminate = false; 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
 
    // Request progress bar 
    requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);//轉圈樣的ProgressBar 
    setContentView(R.layout.progressbar_4); 
    setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可見 
     
    Button button = (Button) findViewById(R.id.toggle); 
    button.setOnClickListener(new Button.OnClickListener() { 
        public void onClick(View v) { 
            mToggleIndeterminate = !mToggleIndeterminate;//如果是false,設成true,如果是true設成false. 
            setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可見 
        } 
    }); 



作者:johnny901114

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.