Android ProgressBarに関する設定説明
17074 ワード
マルチスタイルProgressBar普通円形ProgressBar
このタイプの進捗バーは、メールの送信、ネットワークへの接続など、実行中であることを示すプロセスです.
一般的にXMLレイアウトで定義すればよい.
このとき、スタイルが設定されていないと、円形で、常に回転する進捗バーになります.
各サイズスタイル円形ProgressBar超大号円形ProgressBar
このとき、スタイルのプロパティを設定すると、このProgressBarにはスタイルがあります.ここで、大きいサイズのProgressBarのスタイルは次のとおりです.
小さいサイズの円形ProgressBar
トランペットProgressBarの対応スタイルは:
完全なXML定義は次のとおりです.
タイトル型円形ProgressBar
タイトル型ProgressBarの対応スタイルは:
完全なXML定義は次のとおりです.
コードで実現する;
ロングプログレスバー
レイアウト内のロングプログレスバー
①まずXMLでレイアウト
説明:
style="?android:attr/progressBarStyleHorizontal"
スタイルをロングに設定
android:max="100"
最大進捗値は100
android:progress="50"
初期化の進捗値
android:secondaryProgress="70"
初期化された最下位レベルの2番目の進捗値
android:layout_gravity="center_vertical"
垂直方向中央揃え
②コード運用
ページタイトルのロングプログレスバーコード実装:①まずウィンドウスタイルプロパティを設定する
②次に進捗値を設定する
ProgressDialog ProgressDialogの円形進捗バー
ProgressDialogは、一般的にシステムタスクやタスクを開くときの進捗状況を表すために使用され、少し待つ意味があります.コード実装:
ProgressDialogのロングプログレスバー
コード実装:
AlertDialog.Builder
AlertDialogの円形ProgressBar
①まずLayoutを設計し、後でViewとしてAlertDialog.Builderを加える
②コード
AlertDialogのロングProgressBar(制御可能)
①まずLayoutを設計し、後でViewとしてAlertDialog.Builderを加える
②コード
③進捗バー進捗値のボタンイベント
App Widgetの進捗バー
Widgetの円形ProgressBar
これは簡単で、Widgetではあまり意味がなく、もう説明しません.
Widgetでのロングプログレスバー(制御可能)
Widgetの実装は繰り返されません.Widgetのレイアウトを設定したとします.デスクトップに上記の図のようなスタイルを追加することもできます.
次に、ボタンイベントと進捗バーのインタラクションを実現します.
以下、Widgetの設計と導入について簡単に説明します.
①設計Widgetレイアウト
②.res/xmlディレクトリを追加しappwidget-providerに追加
③AppWidgetProviderサブクラスの実装
④マニフェストを設定して登録する
ここでは、ボタンと進捗バーのインタラクションを実現します.(Widget自己放送送受信)
①ボタンのメッセージ送信
②Widget自身のメッセージ受信、intent.getAction()を使用してActionを取得
③進捗バーの進捗値設定
ただし、Widget自身のonReceive()メソッドは、他のActionブロードキャストを受信する場合に使用します.では、ManifestでIntent-filterにActionを追加する必要があります.
このタイプの進捗バーは、メールの送信、ネットワークへの接続など、実行中であることを示すプロセスです.
一般的にXMLレイアウトで定義すればよい.
<progressBar android:id="@+id/widget43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
</ProgressBar>
このとき、スタイルが設定されていないと、円形で、常に回転する進捗バーになります.
各サイズスタイル円形ProgressBar超大号円形ProgressBar
このとき、スタイルのプロパティを設定すると、この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"
初期化された最下位レベルの2番目の進捗値
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);
//
②次に進捗値を設定する
etProgress(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でのロングプログレスバー(制御可能)
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);
}
}
④マニフェストを設定して登録する
<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>