Android 自定义进度条
作者:互联网
一。在drawable中新建progressbar.xml,文件名可以更改为其它的,设置进度条的图片,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/prog_back">
</item>
<item android:id="@android:id/progress" android:drawable="@drawable/prog_fore"> </item>
</layer-list>
二。在layout中建一个专用的xml文件,如main.xml,这个文件不能做其它的用途。内容如下:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android=http://schemas.android.com/apk/res/android
android:id="@+id/proVol"
android:progressDrawable="@drawable/progressbar"
style="?android:attr/progressBarStyleHorizontal"
/>
三。在代码中添加进度条的处理:
private ProgressBar proVol = null;
LayoutInflater myInflater = LayoutInflater.from(this);
proVol = (ProgressBar)myInflater.inflate(R.layout.main, null).findViewById(R.id.proVol);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
proVol.setMax(am.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
proVol.setProgress(am.getStreamVolume(AudioManager.STREAM_SYSTEM));
Bitmap bitmapProgBack = BitmapFactory.decodeResource(getResources(),R.drawable.prog_back);
//确定这个控件的大小和位置
left = 124*nDisplayWidth/main.nDefaultWidth;
top = 94*nDisplayHeight/main.nDefaultHeigh;
width = bitmapProgBack.getWidth();
height = bitmapProgBack.getHeight();
lpParam = new AbsoluteLayout.LayoutParams(width,height,left,top);
abslayout.addView(proVol, lpParam);
标签:xml,自定义,AudioManager,进度条,am,proVol,bitmapProgBack,Android,main 来源: https://blog.51cto.com/u_15298588/3034414