Android 如何将background.png设置为进度条的背景,将进度设置为拉伸并成为填充到最大值的部分?
我有进度条,我有两个图像Android 如何将background.png设置为进度条的背景,将进度设置为拉伸并成为填充到最大值的部分?,android,android-layout,progress-bar,android-drawable,android-progressbar,Android,Android Layout,Progress Bar,Android Drawable,Android Progressbar,我有进度条,我有两个图像background.png和progress.png。 如何将background.png设置为进度条的背景,将进度设置为拉伸并成为填充到最大值的部分 我试过像android:progressDrawable=“@drawable/progress”,但它不起作用。我认为为此,您需要为android:progressBarStyleHorizontal设置一个完整的样式 如果转到并只下载设置进度条的主题,您可以看到所需的所有值,因为您正在尝试将自己的图像放入进度条,那么
background.png
和progress.png
。
如何将background.png设置为进度条的背景,将进度设置为拉伸并成为填充到最大值的部分
我试过像android:progressDrawable=“@drawable/progress”,但它不起作用。我认为为此,您需要为
android:progressBarStyleHorizontal设置一个完整的样式
如果转到并只下载设置进度条的主题,您可以看到所需的所有值,因为您正在尝试将自己的图像放入进度条,那么您应该创建自定义进度条,这将帮助您将自己的图像设置为背景和进度图像
在这里,我将为您介绍如何执行自定义ProgressBar。创建一个XML,将background.png作为进程栏的背景,将progress.png作为进程栏的进度指示器,并使用层列表属性…将XML命名为progressbar.XML
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
<!--setting background.png as background of proressbar-->
android:drawable="@drawable/background"/>
<item
android:id="@+id/progress"
<!--setting progress.png as progress drawable of proressbar-->
android:drawable="@drawable/progress">
</item>
</layer-list>
在Style.xml中为自定义进度条创建样式属性
<resources>
<style name="Widget"></style>
<style name="Widget.ProgressBar">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateBehavior">repeat</item>
<item name="android:indeterminateDuration">3500</item>
<item name="android:minWidth">48dip</item>
<item name="android:maxWidth">48dip</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
<style name="Widget.ProgressBar.RegularProgressBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@drawable/progressbar</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">1dip</item>
<item name="android:maxHeight">10dip</item>
</style>
</resources>
真的
重复
3500
48dip
48dip
48dip
48dip
假的
@可拉拔/前进杆
@android:可绘制/进度\不确定\水平
1dip
10度
将自定义CustomProgressBar添加到XML中。假设您将CustomProgressBar.java文件放在com.widgets.custom包中……那么进度条的xml将是
<com.widgets.custom.CustomProgressBar
android:id="@+id/regularprogressbar"
style="@style/Widget.ProgressBar.RegularProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" />
在活动类中,初始化自定义进度条并根据需要使用。但为了更好地理解,我正在上演示活动课
public class ProgressBarActivity extends Activity {
private SaundProgressBar mRegularProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegularProgressBar = (SaundProgressBar) findViewById(R.id.regularprogressbar);
new UpdateBarTask().execute();
}
private class UpdateBarTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
int max = mRegularProgressBar.getMax();
for (int i = 0; i <= max; i++) {
try {
// update every second
Thread.sleep(100);
} catch (InterruptedException e) {
}
publishProgress(i);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
mRegularProgressBar.setProgress(values[0]);
}
}
}
公共类ProgressBarActivity扩展活动{
私人桑拿按摩吧;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegularProgressBar=(SaundProgressBar)findViewById(R.id.regularprogressbar);
新建UpdateBarTask().execute();
}
私有类UpdateBarTask扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
int max=mRegularProgressBar.getMax();
对于(int i=0;i i i设置为android:progressBarStyleHorizontal,但如何将默认图像更改为Mine您应该能够通过从该站点生成的样式将图像设置为您的样式。一旦将该属性设置为您的样式,您将创建一个样式,其父级为android:Widget.ProgressBar.Horizontal。必要的属性包括在“值”目录中的样式和主题文件中提供给您
public class ProgressBarActivity extends Activity {
private SaundProgressBar mRegularProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegularProgressBar = (SaundProgressBar) findViewById(R.id.regularprogressbar);
new UpdateBarTask().execute();
}
private class UpdateBarTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
int max = mRegularProgressBar.getMax();
for (int i = 0; i <= max; i++) {
try {
// update every second
Thread.sleep(100);
} catch (InterruptedException e) {
}
publishProgress(i);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
mRegularProgressBar.setProgress(values[0]);
}
}
}