普通圓形ProgressBar
該類型進度條也就是一個表示運轉的過程,例如發送短信,連接網絡等等,表示一個過程正在執行中。
一般只要在XML布局中定義就可以了。
- <progressBar android:id="@+id/widget43"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical">
- </ProgressBar>
此時,沒有設置它的風格,那么它就是圓形的,一直會旋轉的進度條。
各大小樣式圓形ProgressBar
超大號圓形ProgressBar
此時,給設置一個style風格屬性后,該ProgressBar就有了一個風格,這里大號ProgressBar的風格是:
- style="?android:attr/progressBarStyleLarge"
完整XML定義是:
- <progressBar android:id="@+id/widget196"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleLarge">
- </ProgressBar>
小號圓形ProgressBar
小號ProgressBar對應的風格是:
- style="?android:attr/progressBarStyleSmall"
完整XML定義是:
- <progressBar android:id="@+id/widget108"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleSmall">
- </ProgressBar>
標題型圓形ProgressBar
標題型ProgressBar對應的風格是:
- style="?android:attr/progressBarStyleSmallTitle"
完整XML定義是:
- <progressBar android:id="@+id/widget110"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleSmallTitle">
- </ProgressBar>
代碼中實現:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- //請求窗口特色風格,這里設置成不明確的進度風格
- setContentView(R.layout.second);
- setProgressBarIndeterminateVisibility(true);
- //設置標題欄中的不明確的進度條是否可以顯示
- }
長形進度條
布局中的長形進度條
①首先在XML進行布局
- <progressBar android:id="@+id/progressbar_updown"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_gravity="center_vertical"
- android:max="100"
- android:progress="50"
- android:secondaryProgress="70" >
講解:
style="?android:attr/progressBarStyleHorizontal" | 設置風格為長形 |
android:max="100" | 最大進度值為100 |
android:progress="50" | 初始化的進度值 |
android:secondaryProgress="70" | 初始化的底層第二個進度值 |
android:layout_gravity="center_vertical" | 垂直居中 |
②代碼中運用
- private ProgressBar myProgressBar;
- //定義ProgressBar
- myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
- //ProgressBar通過ID來從XML中獲取
- myProgressBar.incrementProgressBy(5);
- //ProgressBar進度值增加5
- myProgressBar.incrementProgressBy(-5);
- //ProgressBar進度值減少5
- myProgressBar.incrementSecondaryProgressBy(5);
- //ProgressBar背后的第二個進度條 進度值增加5
- myProgressBar.incrementSecondaryProgressBy(-5);
- //ProgressBar背后的第二個進度條 進度值減少5
頁面標題中的長形進度條
代碼實現:
①先設置一下窗口風格特性
- requestWindowFeature(Window.FEATURE_PROGRESS);
- //請求一個窗口進度條特性風格
- setContentView(R.layout.main);
- setProgressBarVisibility(true);
- //設置進度條可視
②然后設置進度值
- setProgress(myProgressBar.getProgress() * 100);
- //設置標題欄中前景的一個進度條進度值
- setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);
- //設置標題欄中后面的一個進度條進度值
- //ProgressBar.getSecondaryProgress() 是用來獲取其他進度條的進度值
ProgressDialog
ProgressDialog中的圓形進度條
ProgressDialog一般用來表示一個系統任務或是開啟任務時候的進度,有一種稍等的意思。
代碼實現:
- ProgressDialog mypDialog=new ProgressDialog(this);
- //實例化
- mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- //設置進度條風格,風格為圓形,旋轉的
- mypDialog.setTitle("Google");
- //設置ProgressDialog 標題
- mypDialog.setMessage(getResources().getString(R.string.second));
- //設置ProgressDialog 提示信息
- mypDialog.setIcon(R.drawable.android);
- //設置ProgressDialog 標題圖標
- mypDialog.setButton("Google",this);
- //設置ProgressDialog 的一個Button
- mypDialog.setIndeterminate(false);
- //設置ProgressDialog 的進度條是否不明確
- mypDialog.setCancelable(true);
- //設置ProgressDialog 是否可以按退回按鍵取消
- mypDialog.show();
- //讓ProgressDialog顯示
ProgressDialog中的長形進度條
代碼實現:
- ProgressDialog mypDialog=new ProgressDialog(this);
- //實例化
- mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- //設置進度條風格,風格為長形,有刻度的
- mypDialog.setTitle("地獄怒獸");
- //設置ProgressDialog 標題
- mypDialog.setMessage(getResources().getString(R.string.second));
- //設置ProgressDialog 提示信息
- mypDialog.setIcon(R.drawable.android);
- //設置ProgressDialog 標題圖標
- mypDialog.setProgress(59);
- //設置ProgressDialog 進度條進度
- mypDialog.setButton("地獄曙光",this);
- //設置ProgressDialog 的一個Button
- mypDialog.setIndeterminate(false);
- //設置ProgressDialog 的進度條是否不明確
- mypDialog.setCancelable(true);
- //設置ProgressDialog 是否可以按退回按鍵取消
- mypDialog.show();
- //讓ProgressDialog顯示
AlertDialog.Builder
AlertDialog中的圓形ProgressBar
①先來設計一個Layout,待會兒作為一個View,加入AlertDialog.Builder
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <LinearLayout android:id="@+id/LinearLayout01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </LinearLayout>
- <ProgressBar android:layout_gravity="center_vertical|center_horizontal"
- android:layout_height="wrap_content"
- android:progress="57"
- android:id="@+id/myView_ProgressBar2"
- android:layout_width="wrap_content">
- </ProgressBar>
- </LinearLayout>
②代碼羅
- private AlertDialog.Builder AlterD,AlterD2;
- //定義提示對話框
- private LayoutInflater layoutInflater;
- //定義布局過濾器
- private LinearLayout myLayout;
- //定義布局
- layoutInflater2=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
- //獲得系統的布局過濾服務
- myLayout2=(LinearLayout) layoutInflater2.inflate(R.layout.roundprogress, null);
- //得到事先設計好的布局
- AlterD2.setTitle(getResources().getString(R.string.RoundO));
- //設置對話框標題
- AlterD2.setIcon(R.drawable.ma);
- //設置對話框圖標
- AlterD2.setMessage(getResources().getString(R.string.ADDView));
- //設置對話框提示信息
- AlterD2.setView(myLayout2);
- //設置對話框中的View
- AlterD2.show();
- //讓對話框顯示
AlertDialog中的長形ProgressBar(可控制)
①先來設計一個Layout,待會兒作為一個View,加入AlertDialog.Builder
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <Button
- android:layout_height="wrap_content"
- android:text="-"
- android:layout_width="50dp"
- android:id="@+id/myView_BT_Down">
- </Button>
- <ProgressBar
- android:layout_gravity="center_vertical"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:id="@+id/myView_ProgressBar"
- android:progress="57"
- android:layout_width="178dp">
- </ProgressBar>
- <Button android:layout_height="wrap_content"
- android:text="+"
- android:layout_width="50dp"
- android:id="@+id/myView_BT_Up">
- </Button>
- </LinearLayout>
②代碼羅
- private AlertDialog.Builder AlterD,AlterD2;
- //定義提示對話框
- private LayoutInflater layoutInflater;
- //定義布局過濾器
- private LinearLayout myLayout;
- //定義布局
- layoutInflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
- //獲得系統的布局過濾服務
- myLayout=(LinearLayout) layoutInflater.inflate(R.layout.myview, null);
- //得到事先設計好的布局
- myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);
- mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);
- mypro=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
- //通過myLayout.findViewById來獲取自定義View中的Widget控件元素
- myup.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mydown.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mypro.setProgress(Tag);
- //設置一個Tag作為進度值
- AlterD.setTitle(getResources().getString(R.string.RectO));
- //設置對話框標題
- AlterD.setIcon(R.drawable.mb);
- //設置對話框圖標
- AlterD.setMessage(getResources().getString(R.string.ADDView));
- //設置對話框提示信息
- AlterD.setView(myLayout);
- //設置對話框添加的View
- AlterD.setPositiveButton("OK", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- MyProgressBar.Tag=mypro.getProgress();
- }});
- //設置對話框按鈕,以及按鈕的事件監聽器
- AlterD.show();
- //讓對話框顯示
③進度條進度值的按鈕事件
- myup.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mydown.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- 對應的代碼:
- @Override
- public void onClick(View button) {
- // TODO Auto-generated method stub
- SwitchUPorDown(button);
- }
- private void SwitchUPorDown(View button) {
- switch (button.getId()) {
- case R.id.myView_BT_Up: {
- mypro.incrementProgressBy(1);
- }
- break;
- case R.id.myView_BT_Down: {
- mypro.incrementProgressBy(-1);
- }
- break;
- default:
- break;
- }
- }
App Widget中的進度條
Widget中的圓形ProgressBar
這個很簡單,在Widget中沒有多大意思,不再敷述。
Widget中的長形ProgressBar(可控制)
Widget的實現就不再重復,假設您已經把Widget布局,相應設置已經設置好了。也可以在桌面加入類似上面圖中的樣式。
現在我們來實現一下按鈕事件,與進度條的交互。
下面還是簡單講解一下Widget的設計與部署。
①設計Widget布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:background="@drawable/widget"
- android:layout_height="74dp"
- android:layout_width="296dp">
- <Button
- android:layout_height="wrap_content"
- android:text="-"
- android:layout_gravity="center_vertical"
- android:layout_width="50dp"
- android:id="@+id/widget_BT_Down"
- android:layout_marginLeft="10dp">
- </Button>
- <ProgressBar
- android:layout_gravity="center_vertical"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="178dp"
- android:id="@+id/widget_ProgressBar">
- </ProgressBar>
- <Button
- android:layout_height="wrap_content"
- android:text="+"
- android:layout_gravity="center_vertical"
- android:layout_width="50dp"
- android:id="@+id/widget_BT_Up">
- </Button>
- </LinearLayout>
②新增一個.res/xml目錄,加入appwidget-provider
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:initialLayout="@layout/widgetlayout"
- android:updatePeriodMillis="8660000"
- android:minWidth="296dp"
- android:minHeight="74dp">
- </appwidget-provider>
③實現一個AppWidgetProvider子類
- package zyf.test.ProgressBar;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.Context;
- import android.content.Intent;
- public class App extends AppWidgetProvider {
- @Override
- public void onEnabled(Context context) {
- // TODO Auto-generated method stub
- super.onEnabled(context);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- super.onReceive(context, intent);
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- // TODO Auto-generated method stub
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- }
- }
④配置Manifest,進行注冊
- <receiver android:name="AppWidget">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
- </intent-filter>
- <meta-data
- android:resource="@xml/appwidget"
- android:name="android.appwidget.provider">
- </meta-data>
- </receiver>
這里實現按鈕與進度條的交互。(Widget自己廣播發送與接收)
①按鈕的消息發送
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- // TODO Auto-generated method stub
- final int N = appWidgetIds.length;
- // Perform this loop procedure for each App Widget that belongs to this provider
- for (int i=0; i<N; i++) {
- int appWidgetId = appWidgetIds;
- RemoteViews views=
- new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
- Intent UPintent=new Intent("zyf.test.widget.UP");
- Intent DOWNintent=new Intent("zyf.test.widget.DOWN");
- //實例化 兩個帶有Action的Intent
- PendingIntent pendingIntentUp
- =PendingIntent.getBroadcast(context, 0, UPintent, 0);
- PendingIntent pendingIntentDown
- =PendingIntent.getBroadcast(context, 0, DOWNintent, 0);
- //實例化兩個以Intent來構造的PendingIntent
- views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);
- views.setOnClickPendingIntent(R.id.widget_BT_Down, pendingIntentDown);
- //給View上的兩個按鈕綁定事件,這里是廣播消息的發送
- appWidgetManager.updateAppWidget(appWidgetId, views);
- }
- }
②Widget自身消息接收,使用intent.getAction()來獲取Action
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- super.onReceive(context, intent);
- if(intent.getAction().equals("zyf.test.widget.UP")){
- Tag+=5;
- if(Tag>100){
- Tag=100;
- }
- views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
- appManager.updateAppWidget(thisWidget, views);
- }
- if(intent.getAction().equals("zyf.test.widget.DOWN")){
- Tag-=5;
- if(Tag<0){
- Tag=0;
- }
- views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
- appManager.updateAppWidget(thisWidget, views);
- }
- }
③進度條的進度值設置
- views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
- //設置Widget上的進度條的進度值
- //第一個參數,Widget上進度條ID
- //第二個參數,進度條最大值
- //第三個參數Tag,一個int值,就是設置的進度值
- //第四個參數,是否是要進度條不確定
注意了,Widget自身的onReceive()方法如果要接收其他的Action廣播。那就必須在Manifest中,在Intent-filter中添加Action:
- <receiver android:name="AppWidget">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
- <action android:name="zyf.test.widget.UP"></action>
- <action android:name="zyf.test.widget.DOWN"></action>
- </intent-filter>
- <meta-data
- android:resource="@xml/appwidget"
- android:name="android.appwidget.provider">
- </meta-data>
- </receiver>