【问题标题】:Android custom Progress Bar manipulationAndroid自定义进度条操作
【发布时间】:2016-04-06 08:56:29
【问题描述】:

在 Android 中使用自定义 ProgressBars 时,我遇到了一些困难。即,我有两个正确的自定义 ProgressBar 的 .xml 描述。 他们在这里:

  1. custom_progress_bar_horizo​​ntal.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--  @author : @ 
    
    -->
    <layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background">
        <shape>
            <corners
                android:radius="0dip" />
            <stroke
                android:width="1dp"
                android:color="#ff000000" />
            <gradient
                android:startColor="#ffEEEEEE"
                android:centerColor="#ffEEEEEE"
                android:centerY="0.50"
                android:endColor="#ffEEEEEE"
                android:angle="270" />
        </shape>
    </item>
    
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="0dip" />
                <stroke
                    android:width="1dip"
                    android:color="#ff000000" />
                <gradient
                    android:startColor="#ff86CF10"
                    android:endColor="#ff86CF10"
                    android:angle="90" />
            </shape>
        </clip>
    </item>
    

  2. custom_progress_bar_hor_night.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--  @author : @
    
    -->
    <layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background">
        <shape>
            <corners
                android:radius="0dip" />
            <stroke
                android:width="1dp"
                android:color="#ffffffff" />
            <gradient
                android:startColor="#ff000000"
                android:centerColor="#ff000000"
                android:centerY="0.50"
                android:endColor="#ff000000"
                android:angle="270" />
        </shape>
    </item>
    
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="0dip" />
                <stroke
                    android:width="1dip"
                    android:color="#ffffffff" />
                <gradient
                    android:startColor="#ff86CF10"
                    android:endColor="#ff86CF10"
                    android:angle="90" />
            </shape>
        </clip>
    </item>
    

如您所见,这些自定义 ProgressBar 的区别仅在于背景颜色和形状线的颜色。

现在,从 Android 应用(Java 代码)在这两个 .xml 文件(ProgressBar 设计)之间切换的最佳方式是什么?实际上,我想在按下应用屏幕上的按钮时切换 ProgressBar 设计。

我应该有两个 ProgressBar 实例一个在另一个之上并更改它们的可见性(一个可见,另一个不可见,反之亦然),还是有一种更智能的方法来使用单个 ProgressBar 实例进行操作?

编辑:

我知道here 描述的方法。用户使用以下方法根据当前进度值在 ProgressBar 的三个不同 .xml 描述之间进行选择:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="2999" android:drawable="@drawable/progress_horizontal_red" />
    <item android:maxLevel="4999" android:drawable="@drawable/progress_horizontal_yellow" />
    <item android:maxLevel="10000" android:drawable="@drawable/progress_horizontal_green" />
</level-list>

我想实现非常相似的事情。唯一的区别是我不希望当前的进度值改变我在屏幕上可见的 .xml 文件。我希望能够通过按下按钮来更改 ProgressBar 设计。 我觉得android:maxLevel="xxxx" 选择器应该用可以取truefalse 值的东西来改变。我会通过按下一个按钮来切换这个true/false 值。

我们将不胜感激任何建议。

非常感谢您的时间和精力。

真诚地, 博扬。

【问题讨论】:

    标签: java android xml android-progressbar


    【解决方案1】:

    您只需在代码中的ProgressBar 上调用setProgressDrawable(),无需两个进度条。

    progressBar.setProgressDrawable(context.getResources().getDrawable(R.drawable.custom_progress_bar_horizontal));
    
    progressBar.setProgressDrawable(context.getResources().getDrawable(R.drawable.custom_progress_bar_hor_night));
    

    【讨论】:

    • 正中,伙计!这正是我一直在寻找的。你让我开心,为我节省了很多时间。谢谢!
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多