我的布局中有一个搜索栏,并且已在xml文件中将paddings和thumbOffset设置为8dp。
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/graph"
android:layout_marginTop="10dp"
android:max="100"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:thumbOffset="8dp"
android:progress="97"
android:progressDrawable="@drawable/rounded_corner_seekbar" />
我遇到的问题是,当进度接近尾声(但不是终点)时,进度线将在前面(如果进度> 50)或后面(如果进度<50)。此图显示了正在发生的情况(进度分别设置为97和3):
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Wc2hVZS5wbmcifQ==” alt =“在此处输入图像描述”>。
您可以在第一个拇指上看到进度线,在第二个拇指上看到进度线。
如果我将paddings和thumbOffset设置为默认值,则不会发生,但是,拇指不会在Rectagle中开始,而是会在边缘出现。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8xMTRLOS5wbmcifQ==” alt =“在此处输入图像描述”>。
我正在寻找的解决方案是使背景变大的方法,因此拇指将位于内部,但是进度线将沿着拇指的中心。
这是我定义搜索栏背景的方式:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- view background color -->
<solid
android:color="#000000" >
</solid>
<!-- view border color and width -->
<stroke
android:width="2dp"
android:color="#FFFFFF" >
</stroke>
<!-- Here is the corner radius -->
<corners
android:radius="5dp" >
</corners>
</shape>
主要问题是进度线将始终从矩形的边缘开始,而拇指可能不是由于填充和thumbOffset而引起的。如果我找到一种使行的起点和终点与拇指相同的方法,它也将起作用。
刚刚设置的解决方案-android:thumbOffset="0dp"
将代码放入style.xml
<style name="SeekBarMyTheme" parent="android:Widget.Holo.Light.SeekBar">
<item name="android:progressDrawable">@drawable/mytheme_scrubber_progress_horizontal_holo_light</item>
<item name="android:indeterminateDrawable">@drawable/mytheme_scrubber_progress_horizontal_holo_light</item>
<item name="android:thumb">@drawable/mytheme_scrubber_control_selector_holo_light</item>
</style>
在可绘制文件夹中创建布局mytheme_scrubber_control_disabled_holo”>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:drawable="@drawable/mytheme_scrubber_control_disabled_holo" /> <item android:state_pressed="true" android:drawable="@drawable/mytheme_scrubber_control_pressed_holo" /> <item android:state_selected="true" android:drawable="@drawable/mytheme_scrubber_control_focused_holo" /> <item android:drawable="@drawable/mytheme_scrubber_control_normal_holo" /> </selector>
在可绘制文件夹中创建布局mytheme_scrubber_progress_horizontal_holo_light
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/mytheme_scrubber_track_holo_light" /> <item android:id="@android:id/secondaryProgress"> <scale android:scaleWidth="100%" android:drawable="@drawable/mytheme_scrubber_secondary_holo" /> </item> <item android:id="@android:id/progress"> <scale android:scaleWidth="100%" android:drawable="@drawable/mytheme_scrubber_primary_holo" /> </item> </layer-list>
在可绘制文件夹中创建布局mytheme_scrubber_control_selector_holo_light
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:drawable="@drawable/mytheme_scrubber_control_disabled_holo" /> <item android:state_pressed="true" android:drawable="@drawable/mytheme_scrubber_control_pressed_holo" /> <item android:state_selected="true" android:drawable="@drawable/mytheme_scrubber_control_focused_holo" /> <item android:drawable="@drawable/mytheme_scrubber_control_normal_holo" /> </selector>
所有这些之后,以您的appTheme风格编写此行
<item name="android:seekBarStyle">@style/SeekBarMyTheme</item>
这将是您的搜索栏布局:
:-mytheme_scrubber_control_normal_holo,mytheme_scrubber_control_pressed_holo,mytheme_scrubber_control_focused_holo,mytheme_scrubber_control_normal_holo这些均位于可绘制文件夹中,并且均为.png文件。<SeekBar android:id="@+id/songProgressBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="8dp" android:paddingLeft="16dp" android:paddingRight="16dp" />
NOTE
希望它对您有帮助,如有任何疑问,请通知我。