Java Android:如何更改 ProgressBar 的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2739186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:19:17  来源:igfitidea点击:

Android: How do I change the height of a ProgressBar?

javaandroiduser-interfacelayoutprogress-bar

提问by Tomek

I was wondering what is the easiest way to change the height of a ProgressBar in Android?

我想知道在 Android 中更改 ProgressBar 高度的最简单方法是什么?

Thanks,

谢谢,

Tomek

托梅克

采纳答案by Jon W

If your progress bar is defined in the XML layout, it looks like you define its height like so:

如果您的进度条是在 XML 布局中定义的,看起来您像这样定义它的高度:

<ProgressBar  
android:minHeight="20dip" 
android:maxHeight="20dip"/>

However I'm just making a guess from this article.

不过我只是从这篇文章中猜测。

回答by Slickelito

The trick is to have your ProgressBar use the "old", none halo, style. Otherwise it will use a 9-patch image as a progress drawable and you can't change the height unless you manipulate the image. So here is what I did :

诀窍是让您的 ProgressBar 使用“旧”的、无光环的样式。否则,它将使用 9-patch 图像作为可绘制进度,除非您操纵图像,否则您无法更改高度。所以这就是我所做的:

the progressbar :

进度条:

<ProgressBar
  android:id="@+id/my_progressbar"
  style="@android:style/Widget.ProgressBar.Horizontal"
  android:layout_width="fill_parent"
  android:layout_height="10dp"
  android:progressDrawable="@drawable/horizontal_progress_drawable_red" />

the progress drawable (horizontal_progress_drawable_red.xml) :

可绘制进度(horizo​​ntal_progress_drawable_red.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="#808080"/>
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#80ff0000"/>
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#ff0000"/>
            </shape>
        </clip>
    </item>

</layer-list>

回答by rnofenko

You need to replace

你需要更换

style=”?android:attr/progressBarStyleHorizontal”

to

style="@android:style/Widget.ProgressBar.Horizontal"

and layout_height will work

和 layout_height 将工作

android:layout_height="50dp"

回答by Ahmad Aghazadeh

Can use this code :

可以使用此代码:

mProgressBar.setScaleY(3f);

Or use custom style

或者使用自定义样式

values->styles.xml

值->styles.xml

<style name="tallerBarStyle" parent="@android:style/Widget.SeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:minHeight">8dip</item>
    <item name="android:maxHeight">20dip</item>
</style>

Then in your ProgressBar add:

然后在您的 ProgressBar 添加:

style="@style/tallerBarStyle"