In actual work, it is often necessary to change the color, style, and background image of some controls based on the artist's design, the following describes how to customize the background color, progress bar color, and slider image in Android.
First, the main. XML content in the layout folder is as follows:
1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:orientation="vertical" android:layout_width="fill_parent" 4. android:layout_height="fill_parent"> 5. 6. <SeekBar android:id="@+id/seek" android:layout_width="300px" 7. android:layout_height="wrap_content" android:max="100" 8. android:progress="50" android:progressDrawable="@drawable/seekbar_img" 9. android:thumb="@drawable/thumb" /> 10. </LinearLayout>
Note that its Android: progressdrawable = "@ drawable/seekbar_img" and Android: thumb = "@ drawable/thumb" correspond to the picture of the progress bar and the picture of the slider, the image here can also be an XML file in our custom drawable, which can be understood as how the two attributes should be displayed, @ drawable/seekbar_img and @ drawable/thumb correspond to the files seekbar_img.xml and thumb in the drawable folder respectively. XML, which shows how to display the progress bar and slider
Seek_thumb.xml in the system. The content is as follows:
1. <?xml version="1.0" encoding="utf-8"?> 2. <!-- Copyright (C) 2008 The Android Open Source Project 3. 4. Licensed under the Apache License, Version 2.0 (the "License"); 5. you may not use this file except in compliance with the License. 6. You may obtain a copy of the License at 7. 8. http://www.apache.org/licenses/LICENSE-2.0 9. 10. Unless required by applicable law or agreed to in writing, software 11. distributed under the License is distributed on an "AS IS" BASIS, 12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13. See the License for the specific language governing permissions and 14. limitations under the License. 15. --> 16. 17. <!-- This is the thumb on the seek bar. --> 18. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 19. 20. <item android:state_pressed="true" 21. android:state_window_focused="true" 22. android:drawable="@drawable/seek_thumb_pressed" /> 23. 24. <item android:state_focused="true" 25. android:state_window_focused="true" 26. android:drawable="@drawable/seek_thumb_selected" /> 27. 28. <item android:state_selected="true" 29. android:state_window_focused="true" 30. android:drawable="@drawable/seek_thumb_selected" /> 31. 32. <item android:drawable="@drawable/seek_thumb_normal" /> 33. 34. </selector>
Progress_horizontal.xml in the system. The content is as follows:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! -- Copyright (c) 2008 the android open source project
- Licensed under the Apache license, version 2.0 (the "License ");
- You may not use this file before t in compliance with the license.
- You may obtain a copy of the license
- Http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- Distributed under the license is distributed on an "as is" basis,
- Without warranties or conditions of any kind, either express or implied.
- See the license for the specific language governing permissions and
- Limitations under the license.
- -->
- <Layer-list xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Item Android: Id = "@ Android: ID/background">
- <Shape>
- <Corners Android: radius = "5dip"/>
- <Gradient
- Android: startcolor = "# ff9d9e9d"
- Android: centercolor = "# ff5a5d5a"
- Android: centery= "0.75"
- Android: endcolor = "# ff747674"
- Android: Angular = "270"
- />
- </Shape>
- </Item>
- <Item Android: Id = "@ Android: ID/secondaryprogress">
- <Clip>
- <Shape>
- <Corners Android: radius = "5dip"/>
- <Gradient
- Android: startcolor = "#80ffd300"
- Android: centercolor = "#80ffb600"
- Android: centery= "0.75"
- Android: endcolor = "# a0ffcb00"
- Android: Angular = "270"
- />
- </Shape>
- </Clip>
- </Item>
- <Item Android: Id = "@ Android: ID/progress">
- <Clip>
- <Shape>
- <Corners Android: radius = "5dip"/>
- <Gradient
- Android: startcolor = "# ffffd300"
- Android: centercolor = "# ffffb600"
- Android: centery= "0.75"
- Android: endcolor = "# ffffcb00"
- Android: Angular = "270"
- />
- </Shape>
- </Clip>
- </Item>
- </Layer-List>
- With the source code of these two files, we can draw a gourd Based on the samples.
First, create the seek_bar.xml file and thumb. xml file in the drawable folder of your project.
The content of the two files I wrote is as follows: seekbar_img.xml
-
1. <? XML version = "1.0" encoding = "UTF-8"?> 2. <layer-list xmlns: Android = "http://schemas.android.com/apk/res/android"> 3. <! -- Background image --> 4. <item Android: Id = "@ + Android: ID/background" Android: drawable = "@ drawable/BG"/> 5. <! -- All energy diagram --> 6. <item Android: Id = "@ + Android: ID/secondaryprogress" 7. android: drawable = "@ drawable/BG"/> 8. <! -- Progress and energy diagram --> 9. <item Android: Id = "@ + Android: ID/progress" Android: drawable = "@ drawable/bg2"/> 10. </layer-List>
Thumb. xml
-
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
- <! -- Press status -->
- <Item Android: state_pressed = "true" Android: drawable = "@ drawable/bg3"/>
- <! -- Normal no-focus status -->
- <Item Android: state_focused = "false" Android: state_pressed = "false"
- Android: drawable = "@ drawable/bg4"/>
- </Selector>