简体   繁体   English

如何在Android中更改搜索栏颜色?

[英]How to change seekbar color in Android?

Is there a way I can change yellow color in the SeekBar android widget? 有没有办法可以在SeekBar android小部件中更改黄色?

在此输入图像描述

Any soln? 任何溶解?

Step 1: Create Your Image Drawables (9-Patch) 第1步:创建图像绘图(9-Patch)

Before creating any XML drawables, make sure you create the image drawables (including one 9-patch drawable) needed for the seekbar background, handle, and progress sections. 在创建任何XML drawable之前,请确保创建seekbar背景,句柄和进度部分所需的图像drawable(包括一个9-patch drawable)。 The 9-patch drawables will be put to use by the XML drawables in the steps below. 9-patch drawables将在下面的步骤中由XML drawables使用。

Create the following drawables and place them in your /res/drawable/ folder: 创建以下drawable并将它们放在/ res / drawable /文件夹中:

Step 2: SeekBar Progress Drawable 第2步:SeekBar Progress Drawable

Now create an XML drawable for the Android seekbar progress (the blue-striped section in the example), call it seekbar_progress_bg.xml, and place it in your /res/drawable/ folder: 现在为Android搜索栏进度创建一个XML drawable(示例中的蓝色条纹部分),将其命名为seekbar_progress_bg.xml,并将其放在/ res / drawable /文件夹中:

<?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <clip>
            <shape>
                <gradient
                    android:startColor="#FF5e8ea3"
                    android:centerColor="#FF32a0d2"
                    android:centerY="0.1"
                    android:endColor="#FF13729e"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <item>
        <clip>
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@drawable/stripe_bg"
            android:tileMode="repeat"
            android:antialias="true"
            android:dither="false"
            android:filter="false"
            android:gravity="left"
        />
        </clip>
    </item>
</layer-list>

The above XML first draws a semi-transparent, blue gradient, then layers the semi-transparent stripe image on top of the gradient. 上面的XML首先绘制一个半透明的蓝色渐变,然后将半透明条纹图像层叠在渐变的顶部。 The highlighted line of code (line 20) refers to the stripe (semi-transparent) image inside your drawable folder, created in Step 1. 突出显示的代码行(第20行)是指在步骤1中创建的drawable文件夹中的条带(半透明)图像。

For more information on creating custom shapes via XML, check out the Android drawable resources docs, specifically the bitmap and shape sections. 有关通过XML创建自定义形状的更多信息,请查看Android可绘制资源文档,特别是位图和形状部分。

Step 3: SeekBar Background Drawable 第3步:SeekBar背景Drawable

Next create the main seekbar progress drawable; 接下来创建主搜索栏进度drawable; it'll assign a drawable to the seekbar progress and secondaryProgress actions inside your seekbar. 它会为搜索栏中的搜索栏进度和secondaryProgress操作分配一个drawable。 Name your drawable something like seekbar_progress.xml, place it inside your /res/drawable/ folder: 将您的drawable命名为seekbar_progress.xml,将其放在/ res / drawable /文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <nine-patch
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@drawable/seekbar_background"
            android:dither="true"
         />
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#80028ac8"
                    android:centerColor="#80127fb1"
                    android:centerY="0.75"
                    android:endColor="#a004638f"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/seekbar_progress_bg"
    />
</layer-list>

The first bit of highlighted code above (line 8) is referring to the seekbar background image (9-patch drawable) created in Step 1 and (line 29) is referring to the drawable you created above in Step 2. 上面突出显示的代码的第一位(第8行)是指在步骤1中创建的搜索栏背景图像(9-patch drawable),(第29行)是指您在上面步骤2中创建的drawable。

Step 4: Bringing it all together… 第4步:将所有内容整合在一起......

At this point, all you need to do is call your seekbar_progress drawable when declaring your seekbar: 此时,您需要做的就是在声明搜索栏时调用seekbar_progress drawable:

<SeekBar
        android:id="@+id/frequency_slider"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="20"
        android:progress="0"
        android:secondaryProgress="0"
        android:progressDrawable="@drawable/seekbar_progress"
        android:thumb="@drawable/seek_thumb"
/>

The two lines of highlighted code are setting the progress and thumb drawables for the SeekBar item. 两行突出显示的代码正在设置SeekBar项目的进度和拇指绘制。 The @drawable/seekbar_progress refers to the XML drawable created in the previous step. @ drawable / seekbar_progress指的是在上一步中创建的XML drawable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM