1.在res/values/attrs.xml中自定义属性:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CircleSeekBar"> <attr name="android:thumb" /> <attr name="progress_width" format="dimension" /> <attr name="progress_background" format="color" /> <attr name="progress_front" format="color" /> <attr name="progress_max" format="integer" /> <attr name="show_progress_text" format="boolean" /> <attr name="progress_text_stroke_width" format="dimension" /> <attr name="progress_text_color" format="color" /> <attr name="progress_text_size" format="dimension" /> </declare-styleable> </resources>
2.使用attrs.xml中的属性定义自定义布局文件中的CircleSeekBar的属性:(activity_main.xml文件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:circle_seekbar="http://schemas.android.com/apk/res/com.example.android_seekbar02" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.util.CircleSeekBar android:id="@+id/circle_seekbar" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:thumb="@drawable/thumb" circle_seekbar:progress_background="@android:color/darker_gray" circle_seekbar:progress_front="@android:color/holo_blue_light" circle_seekbar:progress_max="1000" circle_seekbar:progress_text_color="@android:color/holo_green_light" circle_seekbar:progress_text_size="30dp" circle_seekbar:progress_text_stroke_width="4dp" circle_seekbar:progress_width="2dp" circle_seekbar:show_progress_text="true" /> </RelativeLayout>
第二行的xmlns是使用xml的命名空间(namespace)有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件。其格式如下:xmlns:前缀= http://schemas.android.com/apk/res/