Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有多种颜色的搜索栏或进度条_Android_Progress Bar_Seekbar - Fatal编程技术网

Android 具有多种颜色的搜索栏或进度条

Android 具有多种颜色的搜索栏或进度条,android,progress-bar,seekbar,Android,Progress Bar,Seekbar,最初我想创建一个这样的条,当进度为零时,它将是一个淡入淡出的颜色,但随着进度的进行,它将在该部分变亮(这是我能解释的最好的情况)最重要的是我想让工具栏同时显示所有颜色。就像我已经建议的那样,我认为你应该选择一个图层列表,然后设置多个可绘制元素。 这方面的主要问题是我需要调整大小。固定大小的解决方案很容易实现。最后!我去执行一项任务就是帮你解决这个问题,所以如果这足够的话,请随意给我那个赏金,哈哈 尝试在布局中使用此选项: <View android:layout_width="f


最初我想创建一个这样的条,当进度为零时,它将是一个淡入淡出的颜色,但随着进度的进行,它将在该部分变亮(这是我能解释的最好的情况)最重要的是我想让工具栏同时显示所有颜色。

就像我已经建议的那样,我认为你应该选择一个图层列表,然后设置多个可绘制元素。
这方面的主要问题是我需要调整大小。固定大小的解决方案很容易实现。

最后!我去执行一项任务就是帮你解决这个问题,所以如果这足够的话,请随意给我那个赏金,哈哈


尝试在布局中使用此选项:

    <View android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight=".20"/>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_weight="0.62">
                <View android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight=".03"/>
                <ProgressBar style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.94"
                    android:progressDrawable="@drawable/progressmask"
                    android:progress="0"
                    android:max="10"
                    android:rotation="180"/>
                <View android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight=".03"/>
            </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight=".18"/>
</LinearLayout>


其中引用了此可绘制文件(progressmask.xml):


这张图片(colorprogress.png)

它所做的是将图像设置为包含进度条的linearlayout的背景。progressbar将半透明黑色遮罩添加到图像中,使其看起来灯光已关闭

注意:为了获得这种效果,我必须调整进度条(即翻转进度条,并将其设置为10个间隔。你必须做一些数学运算才能使进度与图像对齐。例如设置进度((100 trueprogress)/10)。很抱歉,我没有为你做这部分

这就是进度50%时的样子(小x和三角形将在设备上消失)

我希望这能回答你的问题!

剪辑你的“开启”绘图:
在您的“关闭”可抽出式:

使用
res/drawable/custom\u progress\u drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Background -->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/custom_progress_bar_off"/>

    <!-- Secondary progress - this is optional -->
    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/custom_progress_bar_secondary" />
    </item>

    <!-- Progress -->
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/custom_progress_bar_on" />
    </item>

</layer-list>
或者在xml中,使用

android:progressDrawable="@drawable/custom_progress_drawable"
下面是在xml中使用android:max=“10”时的结果:


它有点不合适,但您可以将
setMax()
与类似
10000的东西一起使用,并在调用
setProgress()时进行一些偏移计算
使其更干净。

你实际上不能将进度条设置为不同的颜色。但是,你可以只使用
上的,并获得你想要的效果。你可以只应用一个图层遮罩。我的意思是添加一个相对布局,最初显示为深灰色,即渐变只有一种颜色它是深灰色。现在,使用以编程方式设置左侧的渐变颜色。显然,左侧的颜色将是透明的。了解更多信息。仅此而已。您只需要计算右侧渐变开始的位置,而不是左侧渐变(
透明
)结束的位置

此方法有轻微缺陷,可能无法在所有设备上运行


完美的方法是创建多个
.9png
图像,并每次以编程方式设置进度对话框的可绘制性。

为此,您应该创建9个补丁图像。在我看来,最好创建一个自定义进度条,并根据其进度设置不同的图像/颜色。看看这是否有帮助不得不承认,@UberPrinny的答案比我的好得多。只是为了根据这一点添加一些信息,你应该将最大值设置为10.000注:默认级别为0,已完全剪裁,因此图像不可见。当级别为10000时,图像未剪裁且完全可见。如何将此进度条设置为垂直条水平的?我不知道你用
MyProgressBar.java
做什么。你能解释一下如何使用它吗?
Drawable progressDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.custom_progress_drawable, getTheme());
myProgressBar.setProgressDrawable(progressDrawable);
android:progressDrawable="@drawable/custom_progress_drawable"