读书人

多样式ProgressBar

发布时间: 2012-09-04 14:19:30 作者: rapoo

多式样ProgressBar

多式样ProgressBar

普通圆形ProgressBar





该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中。

一般只要在XML布局中定义就可以了。

? ?? ?android:layout_height="wrap_content"? ?
? ?? ?android:layout_gravity="center_vertical">
</ProgressBar>复制代码此时,没有设置它的风格,那么它就是圆形的,一直会旋转的进度条。

各大小样式圆形ProgressBar


超大号圆形ProgressBar


此时,给设置一个style风格属性后,该ProgressBar就有了一个风格,这里大号ProgressBar的风格是:? ?? ?android:layout_height="wrap_content"
? ?? ?style="?android:attr/progressBarStyleLarge">
</ProgressBar>复制代码

小号圆形ProgressBar




小号ProgressBar对应的风格是:? ?? ?android:layout_height="wrap_content"
? ?? ?style="?android:attr/progressBarStyleSmall">
</ProgressBar>复制代码

标题型圆形ProgressBar






标题型ProgressBar对应的风格是:? ? android:layout_height="wrap_content"
? ? style="?android:attr/progressBarStyleSmallTitle">
</ProgressBar>复制代码代码中实现:? ?? ???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"? ?
最大进度值为100android:progress="50"??
初始化的进度值android:secondaryProgress="70"
初始化的底层第二个进度值
android:layout_gravity="center_vertical"? ?
垂直居中

②代码中运用<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>复制代码②代码罗<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>复制代码②代码罗<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<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子类? ?? ???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>

读书人网 >移动开发

热点推荐