我正在尝试使用ProgressBar作为计量显示器.我认为这将是一个简单的任务,并认为ProgressBar有一个属性设置为垂直,但我没有看到任何东西.
此外,我希望能够在栏的侧面显示标尺,如清楚地指示当前水平.
指针赞赏 - 谢谢!
jag*_*und 54
我最近遇到了对垂直进度条的需求,但无法使用现有的Progress Bar小部件找到解决方案.我遇到的解决方案通常需要扩展当前的Progress Bar或者一个全新的类.我不相信推出一个新课程来实现简单的方向改变是必要的.
本文介绍了一个简单,优雅,最重要的是,实现垂直进度条的无懈可击的解决方案.我将跳过解释并简单地提供一个千篇一律的解决方案.如果您需要更多详细信息,请随时与我联系或在下面发表评论.
在drawable文件夹中创建一个xml(不是drawable-hdpi或drawable-mdpi - 将它放在drawable中).在本例中,我调用了我的xml vertical_progress_bar.xml
这是放在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">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="180"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip android:clipOrientation="vertical" android:gravity="bottom">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="180"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip android:clipOrientation="vertical" android:gravity="bottom">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="180"
/>
</shape>
</clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
创建一个名为styles.xml的xml文件,并将其放在res/values中.如果您的项目已在res/values中包含styles.xml,则跳过此步骤.
修改styles.xml文件并将以下代码附加到文件末尾:
<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.Vertical">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@drawable/progress_bar_vertical</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minWidth">1dip</item>
<item name="android:maxWidth">12dip</item>
</style>
Run Code Online (Sandbox Code Playgroud)
将新的垂直进度条添加到布局中.这是一个例子:
<ProgressBar
android:id="@+id/vertical_progressbar"
android:layout_width="12dip"
android:layout_height="300dip"
style="@style/Widget.ProgressBar.Vertical"
/>
Run Code Online (Sandbox Code Playgroud)
这应该是您在项目中使用垂直进度条所需要做的全部工作.(可选)您可能具有用于进度条的自定义可绘制九补丁图像.您应该在progress_bar_vertical.xml文件中进行适当的更改.我希望这可以帮助你完成你的项目!
Ram*_*ula 30
您必须创建自己的自定义进度条.
在你的xml中添加这个布局:
<com.example.component.VerticalProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/verticalRatingBar1"
android:layout_width="wrap_content"
android:progress="50"
android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
VerticalProgressBar.java
public class VerticalProgressBar extends ProgressBar{
private int x, y, z, w;
@Override
protected void drawableStateChanged() {
// TODO Auto-generated method stub
super.drawableStateChanged();
}
public VerticalProgressBar(Context context) {
super(context);
}
public VerticalProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
this.x = w;
this.y = h;
this.z = oldw;
this.w = oldh;
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setSelected(true);
setPressed(true);
break;
case MotionEvent.ACTION_MOVE:
setProgress(getMax()
- (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_UP:
setSelected(false);
setPressed(false);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
@Override
public synchronized void setProgress(int progress) {
if (progress >= 0)
super.setProgress(progress);
else
super.setProgress(0);
onSizeChanged(x, y, z, w);
}
}
Run Code Online (Sandbox Code Playgroud)
或者: Jagsaund 解决方案也很完美.
Dan*_* S. 18
我知道这是一个老帖子,但我发现这个问题的一个非常简单的解决方案,可能可以帮助某人.首先创建一个progress_drawable_vertical.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">
<color android:color="#777" />
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom">
<shape>
<gradient
android:startColor="#00FF00"
android:centerColor="#FFFF00"
android:endColor="#FF0000"
android:angle="90" />
</shape>
</clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
然后在progressBar中使用它:
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="100"
android:progress="33"
android:progressDrawable="@drawable/progress_drawable_vertical" />
Run Code Online (Sandbox Code Playgroud)
我还创建了一个progress_drawable_horizontal.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">
<color android:color="#777" />
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left">
<shape>
<gradient
android:startColor="#00FF00"
android:centerColor="#FFFF00"
android:endColor="#FF0000" />
</shape>
</clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
与mantain的目标是progress_drawable_vertical.xml中定义的相同样式
这里的关键是正确使用android:clipOrientation和android:gravity.
我在这里找到了这个解决方案,解决方案的核心类似于jagsaund,但更简单一点.
我找到了可能最好(最简单和最通用)的解决方案:)
这是一篇旧帖子,但我很难找到这个如此简单的解决方案,所以我想我应该发布它..
只需使用scale-drawable(或 9 个补丁,如果您愿意),无需任何其他代码。
例子:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@color/transparent"/>
<item android:id="@android:id/progress">
<scale android:scaleGravity="bottom" android:scaleWidth="0%" android:scaleHeight="100%">
<shape >
<solid android:color="@color/blue"/>
<corners android:topRightRadius="1dp" android:topLeftRadius="1dp"/>
</shape>
</scale>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
当然还有正常的代码:
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="24dp"
android:layout_height="match_parent"
android:max="1000"
android:progress="200"
android:progressDrawable="@drawable/progress_scale_drawable" />
Run Code Online (Sandbox Code Playgroud)
注意scale-drawable的 xml 行(魔术行):
android:scaleGravity="bottom" //scale from 0 in y axis (default scales from center Y)
android:scaleWidth="0%" //don't scale width (according to 'progress')
android:scaleHeight="100%" //do scale the height of the drawable
Run Code Online (Sandbox Code Playgroud)
This perfectly works
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#DDDDDD"
android:centerColor="#DDDDDD"
android:centerY="0.75"
android:endColor="#DDDDDD"
android:angle="270"
/>
</shape>
</item>
<item
android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="top">
<shape>
<gradient
android:angle="0"
android:startColor="#302367"
android:centerColor="#7A5667"
android:endColor="#C86D67"/>
</shape>
</clip>
</item>
</layer-list>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="5dp"
android:layout_height="match_parent"`enter code here`
android:progressDrawable="@drawable/progress_dialog"/>
Run Code Online (Sandbox Code Playgroud)
简单易行的方法:
只需向 a 添加一个视图LinearLayout并对其进行缩放即可。
<LinearLayout
android:layout_width="4dp"
android:layout_height="300dp"
android:background="@color/md_green_50"
android:orientation="vertical">
<View
android:id="@+id/progressView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="1"
android:background="@color/md_green_500"
android:scaleY="0.0" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
将视图设置pivotY为零:
progressView.pivotY = 0F
Run Code Online (Sandbox Code Playgroud)
现在您可以使用和scaleY之间的内容填充进度:0F1F
progressView.scaleY = 0.3F
Run Code Online (Sandbox Code Playgroud)
奖励:
使用动画进度animate():
progressView.animate().scaleY(0.3F).start()
Run Code Online (Sandbox Code Playgroud)
创建进度条(我将代码从 C# 转换为 Java,因此可能不是 100% 正确)
ProgressBar progBar = new ProgressBar(Context, null, Android.resource.attribute.progressDrawable);
progBar.progressDrawable = ContextCompat.getDrawable(Context, resource.drawable.vertical_progress_bar);
progBar.indeterminate = false;
Run Code Online (Sandbox Code Playgroud)
垂直进度条.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">
<shape>
<solid android:color="@color/grey" />
<corners android:radius="20dip" />
</shape>
</item>
<item android:id="@android:id/progress">
<scale
android:drawable="@drawable/vertical_progress_bar_blue_progress"
android:scaleHeight="100%"
android:scaleGravity="bottom"/>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
Vertical_progress_bar_blue_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:radius="20dip" />
<solid android:color="@color/ProgressBarFourth" />
</shape>
Run Code Online (Sandbox Code Playgroud)
使您的栏垂直的是中的scaleHeight和scaleGravity属性vertical_progress_bar.xml。
它最终看起来像这样:
Rom*_*Guy -18
默认情况下不支持垂直进度条。
| 归档时间: |
|
| 查看次数: |
38388 次 |
| 最近记录: |