android 自定义progressbar 样式
在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <!-- 背景 gradient是渐变,corners定义的是圆角 -->
<item android:id="@android:id/background"
android:layout_width="wrap_content">
<shape>
<corners android:radius="10dp" /> <solid android:color="#ffffff" />
</shape>
</item>
<!-- 第二条进度条颜色 -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="10dp" /> <gradient
android:angle="90.0"
android:centerColor="#aadfdf"
android:centerY="0.45"
android:endColor="#aadfdf"
android:startColor="#aadfdf" />
</shape>
</clip>
</item>
<!-- 进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dp" />
<solid android:color="#00a8a7" />
</shape>
</clip>
</item> </layer-list>
progressBar style
style="?android:attr/progressBarStyleHorizontal"
<ProgressBar
android:id="@+id/my_progress"
android:layout_width="match_parent"
android:layout_height="12dp"
android:max="100"
android:progress="40"
android:secondaryProgress="70"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/progressbar_color"/>
dialog自定义样式XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@drawable/bar">
<TextView
android:id="@+id/progress_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="44px"
android:text="download"
android:layout_marginTop="113px"
android:layout_gravity="center"
android:textColor="#282828"
/> <ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="18dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_centerHorizontal="true"
android:progressDrawable="@drawable/bar"
android:secondaryProgress="100"
/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/progress_percent"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="30px"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp" android:textColor="#282828"
/>
<TextView
android:id="@+id/progress_number"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="100dp"
android:textSize="30px"
android:gravity="center_horizontal"
android:textColor="#282828"
/>
</LinearLayout>
</LinearLayout> </LinearLayout>
CommonProgressDialog.java类:
package buzz.things.prigressdialog; import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.widget.ProgressBar;
import android.widget.TextView; import java.text.NumberFormat; /**
* Created by buzz on 2015/6/12.
*/
public class CommonProgressDialog extends AlertDialog { private ProgressBar mProgress;
private TextView mProgressNumber;
private TextView mProgressPercent;
private TextView mProgressMessage; private Handler mViewUpdateHandler;
private int mMax;
private CharSequence mMessage;
private boolean mHasStarted;
private int mProgressVal; private String TAG="CommonProgressDialog";
private String mProgressNumberFormat;
private NumberFormat mProgressPercentFormat;
public CommonProgressDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
initFormats();
} @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar_sweet);
mProgress=(ProgressBar) findViewById(R.id.progress);
mProgressNumber=(TextView) findViewById(R.id.progress_number);
mProgressPercent=(TextView) findViewById(R.id.progress_percent);
mProgressMessage=(TextView) findViewById(R.id.progress_message);
// LayoutInflater inflater = LayoutInflater.from(getContext());
mViewUpdateHandler = new Handler() { @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int progress = mProgress.getProgress();
int max = mProgress.getMax();
double dProgress = (double)progress/(double)(1024 * 1024);
double dMax = (double)max/(double)(1024 * 1024);
if (mProgressNumberFormat != null) {
String format = mProgressNumberFormat;
mProgressNumber.setText(String.format(format, dProgress, dMax));
} else {
mProgressNumber.setText("");
}
if (mProgressPercentFormat != null) {
double percent = (double) progress / (double) max;
SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mProgressPercent.setText(tmp);
} else {
mProgressPercent.setText("");
}
} };
// View view = inflater.inflate(R.layout.common_progress_dialog, null);
// mProgress = (ProgressBar) view.findViewById(R.id.progress);
// mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
// mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
// setView(view);
//mProgress.setMax(100);
onProgressChanged();
if (mMessage != null) {
setMessage(mMessage);
}
if (mMax > 0) {
setMax(mMax);
}
if (mProgressVal > 0) {
setProgress(mProgressVal);
}
}
private void initFormats() {
mProgressNumberFormat = "%1.2fM/%2.2fM";
mProgressPercentFormat = NumberFormat.getPercentInstance();
mProgressPercentFormat.setMaximumFractionDigits(0);
}
private void onProgressChanged() {
mViewUpdateHandler.sendEmptyMessage(0); }
public void setProgressStyle(int style) {
//mProgressStyle = style;
}
public int getMax() {
if (mProgress != null) {
return mProgress.getMax();
}
return mMax;
}
public void setMax(int max) {
if (mProgress != null) {
mProgress.setMax(max);
onProgressChanged();
} else {
mMax = max;
}
}
public void setIndeterminate(boolean indeterminate) {
if (mProgress != null) {
mProgress.setIndeterminate(indeterminate);
}
// else {
// mIndeterminate = indeterminate;
// }
}
public void setProgress(int value) {
if (mHasStarted) {
mProgress.setProgress(value);
onProgressChanged();
} else {
mProgressVal = value;
}
} @Override
public void setMessage(CharSequence message) {
// TODO Auto-generated method stub
//super.setMessage(message);
if(mProgressMessage!=null){
mProgressMessage.setText(message);
}
else{
mMessage = message;
}
} @Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mHasStarted = true;
} @Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mHasStarted = false;
} }
测试程序:
private void showDialog(){
mDialog = new CommonProgressDialog(this);
mDialog.setMessage("正在下载");
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
//cancel(true);
}
});
mDialog.show();
mDialog.setMax(100*1024*1024);
mDialog.setProgress(65*1024*1024);
}
效果图

ref:http://blog.csdn.net/heqiangflytosky/article/details/21176943?utm_source=tuicool
ref:http://blog.csdn.net/qjlhlh/article/details/7979179
android 自定义progressbar 样式的更多相关文章
- Android自定义ProgressBar样式
我们使用的进度条多种多样,下面有几种自定义的进度条的样式,下面介绍几个. 进度条的有基本的四种样式: 默认风格的进度条: android:progressBarStyle 水平长型进度条: andro ...
- Android: 自定义Tab样式,一种简单的方式。
之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法. 1.制作4个9patch的tab样式,可参考android默认的资源 tab_unselected.9.pn ...
- Android自定义progressBar
通过继承系统ProgressBar实现 效果图 实现 HorizontalProgressBarWithNumber 自定义属性 <?xml version="1.0" en ...
- Android 自定义title样式
requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性.参数是Window类中定义的常量.一.枚举常量1.DEFAULT_FEATURES:系统默认状态,一般不需 ...
- Android -- 自定义ProgressBar图片
注:所有的进度条都要配置 android:indeterminate="false" android:indeterminateDrawable="样式文件 ...
- android自定义TabWidget样式
先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...
- Android 自定义CheckBox 样式
新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式. <?xml version="1.0" encoding=" ...
- 转:android 自定义RadioButton样式
http://gundumw100.iteye.com/blog/1146527 上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用Ra ...
- Android 自定义CheckBox样式
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml. <selector xmlns:android="http://schemas.and ...
随机推荐
- 屏蔽NumberPicker点击可输入问题
1.xml布局中添加属性:Android:descendantFocusability="blocksDescendants" 2.代码中设置:numberPicker.setDe ...
- 倍福TwinCAT(贝福Beckhoff)基础教程1.2 TwinCAT安装配置
由于TC2和TC3都有可能用到,个人推荐都安装,但是注意必须是先安装的TwinCAT2,然后安装TwinCAT3,如果反了可能两个都没法用(打开TcSwitchRuntime提示Both TwinCA ...
- 微信小程序 - .gitignore失效问题
-------------------------------------------- Last Update Date:2018-8-8 ----------------------------- ...
- Laravel请求/Cookies/文件上传
一.HTTP请求 1.基本示例:通过依赖注入获取当前 HTTP 请求实例,应该在控制器的构造函数或方法中对Illuminate\Http\Request 类进行类型提示,当前请求实例会被服务容器自动注 ...
- highCharts怎样实现json数组数据的图形展示
昨天花了一天的时间学习了一下highcharts.主要的内容差点儿相同都看了一遍,然后试着写了一个完整的demo,期间可谓百转千回.费了不少功夫.终于还是实现了我所想要的效果图,接下来我将怎样实现统计 ...
- discuz !NT 3.5 论坛整合 .net 网站用户登录,退出
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlCont ...
- PDF快速创建目录
很多从网上直接下载的PDF电子书目录都不全,因此搜索资料又加以改化,总结了一个自己手动快速创建目录的办法,分享给大家. 百度搜索PDF电子书的目录或者直接从PDF拷贝到Notepad++等编辑器,使用 ...
- SQL的四种连接
SQL的四种连接-内连接.左外连接.右外连接.全连接 今天在看一个遗留系统的数据表的时候发现平时查找的视图是FULL OUT JOIN的,导致平时的数据记录要进行一些限制性处理,其实也可以设置视图 ...
- python单元测试框架 pyunit
概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的放置位 ...
- Mysql-Proxy实现mysql读写分离、负载均衡 (转)
在mysql中实现读写分离.负载均衡,用Mysql-Proxy是很容易的事,不过大型处理对于性能方面还有待提高,主要配置步骤如下: 1.1. mysql-proxy安装 MySQL Proxy就是这么 ...