Android: How do I change the height of a ProgressB

2020-02-25 07:46发布

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

Thanks,

Tomek

4条回答
Emotional °昔
2楼-- · 2020-02-25 07:50

Can use this code :

mProgressBar.setScaleY(3f);

Or use custom style

values->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:

style="@style/tallerBarStyle"
查看更多
家丑人穷心不美
3楼-- · 2020-02-25 07:52

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 :

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) :

<?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>
查看更多
乱世女痞
4楼-- · 2020-02-25 07:58

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

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

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

查看更多
趁早两清
5楼-- · 2020-02-25 08:03

You need to replace

style=”?android:attr/progressBarStyleHorizontal”

to

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

and layout_height will work

android:layout_height="50dp"
查看更多
登录 后发表回答