【问题标题】:Android: How do I change the height of a ProgressBar?Android:如何更改 ProgressBar 的高度?
【发布时间】:2011-02-13 21:57:36
【问题描述】:

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

谢谢,

托梅克

【问题讨论】:

    标签: java android user-interface layout progress-bar


    【解决方案1】:

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

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

    不过,我只是根据this article 进行猜测。

    【讨论】:

    • 自动添加到操作栏的 API >11 怎么样?
    【解决方案2】:

    你需要更换

    style=”?android:attr/progressBarStyleHorizontal”
    

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

    layout_height 会起作用

    android:layout_height="50dp"
    

    【讨论】:

    • 工作就像一个魅力!谢谢
    • 如何添加颜色等属性?!
    • 您可以在 SeekBar XML 中使用“android:progressTint”和“android:progressBackgroundTint”设置进度的颜色和进度背景。
    • @Luke 你如何为 API 21 之前的设备做到这一点?
    【解决方案3】:

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

    进度条:

    <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" />
    

    进度可绘制(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>
    

    【讨论】:

    • 进度可绘制的根元素应该是什么?编辑:
    【解决方案4】:

    可以使用这个代码:

    mProgressBar.setScaleY(3f);
    

    或者使用自定义样式

    值->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>
    

    然后在你的 ProgressBar 添加:

    style="@style/tallerBarStyle"
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2019-04-09
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2017-08-04
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多