Android ProgressBar UI 自定义布局 [英] Android ProgressBar UI custom layout
问题描述
我正在尝试自定义一个状态栏,使其看起来像这个图像:
I am trying to customize a status bar so it will look like this image:
然而,几个小时后,我只有一个状态栏,我只需要背景(图 2):
However, after a good couple of hours all I have is a status bar with the background I need only (Image 2):
我目前的代码是:xml布局:
The current code I have is: xml layout:
<ProgressBar
android:id="@+id/pBarOverallStatus"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_weight="94"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="@drawable/progress_bar_states" >
</ProgressBar>
progress_bar_states.xml:
The progress_bar_states.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- <item android:id="@android:id/progress">
<bitmap
android:gravity="center"
android:src="@drawable/progressbar_progressing" />
<corners android:radius="10dp" />
</item>
-->
</layer-list>
删除评论,其中 proressbar_progressing 看起来像然后我有一些难看的东西,因为没有角落.
Removing the comment, where proressbar_progressing looks like then I am having something a bit ugly as there are no corners.
我正在从代码中添加背景,例如:
I am adding the background from code, something like:
overallStatus = (ProgressBar) findViewById(R.id.pBarOverallStatus);
Resources res = getResources();
overallStatus.setBackgroundDrawable(res.getDrawable(R.drawable.progressbar_background));
overallStatus.setProgress(50);
我尝试为图像添加角,但没有成功.有人知道我在这里做错了什么吗?或者我错过了什么?另外,您知道如何添加左右环吗?资源:
I tried to add corners to the image but no luck. Does anybody have any idea what I am doing wrong here? Or what am I missing? Also, do you have any idea how could I add the left and right rings? The resource:
推荐答案
解决方案(回答你自己的感觉很奇怪):首先,一个问题是进度可绘制与背景的大小不同(愚蠢的我!).此外,对于可绘制的进度,需要一个剪辑 xml.类似于progressbar_progress_clip.xml:
The solution (feels weird answering your own): First, one problem was that progress drawable had a different size than background (stupid me!). Also, for progress drawable a clip xml was needed. Something like progressbar_progress_clip.xml:
<?xml version="1.0" encoding="utf-8"?>
<clip
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progressbar_progressing"
android:clipOrientation="horizontal"
android:gravity="left"/>
然后,将进度条和两张图片相对布局添加,这样图片就可以在状态栏之后绘制了.像这样:
Then, add the progress bar and two images in a relative layout, so that the images can be drawn after the status bar. Something like this:
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_weight="94" >
<ProgressBar
android:id="@+id/pBarOverallStatus"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:indeterminateOnly="false"
android:max="100"
android:progressDrawable="@drawable/progressbar_progress_clip" >
</ProgressBar>
<ImageView
android:id="@+id/ringLeft"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="7dp"
android:contentDescription="@string/status_bar_ring"
android:src="@drawable/status_bar_ring" />
<ImageView
android:id="@+id/ringRight"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:contentDescription="@string/status_bar_ring"
android:src="@drawable/status_bar_ring" />
</RelativeLayout>
谢谢,伙计们!
这篇关于Android ProgressBar UI 自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!