seekbar thumb center not at the start point

天涯浪子 提交于 2019-12-07 06:09:12

问题


as my pic,the seekbar progress =0,but the seekbar thumb center not at the start point.my code:

   <SeekBar  android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:progressDrawable="@drawable/seekbar_progress"
                 android:thumb="@drawable/seekbarthumb"
                 android:minHeight="10dip"
                 android:maxHeight="10dip"
                 android:thumbOffset="0px"
                 android:max="100"
                 android:id="@+id/bright_seekbar"
                 android:layout_marginLeft="60dip"/>

seekbar_progress

 <?xml version="1.0" encoding="utf-8"?>
 <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:angle="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:angle="270" 
            /> 
        </shape> 
    </clip> 
</item> 
<item android:id="@android:id/progress"> 
    <clip> 
        <shape> 
            <corners android:radius="5dip" /> 
            <gradient 
                    android:startColor="#FFD700" 
                    android:centerColor="#FFB90F" 
                    android:centerY="0.75" 
                    android:endColor="#FFA500" 
                    android:angle="270" 
            /> 
        </shape> 
    </clip> 
 </item> 


</layer-list>

seekbarthumb.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@drawable/pressed_button__volume" />
<item android:state_focused="true" 
      android:drawable="@drawable/pressed_button__volume" />
<item
     android:drawable="@drawable/default_button__volume" />     
</selector>

default_button__volume is image


回答1:


finally found the answer, the case here is that android:thumbOffset is NOT a setter for the thumb place along with the progress bar, but, it's the thumb length from it's beginning to it's center point so that the seekbar can adjust the thumb's center point at it's beginning point. So, if we have a thumb image with a width of 50, the thumbOffset has to be 25 (50/2), so that the seekbar will adjust the zero value at the pixel 25 of the thumb which will make the thumb appears to be exactly at the middle. thanks for that man who redirect me to know that.




回答2:


Your thumb image needs to have it's yellow dot at the center (horizontally); add transparent pixels left or right to make it so. Then, change android:thumbOffset to be exactly half your thumb image size. For example, if your image is 10dp in width, set android:thumbOffset to 5dp.




回答3:


For me solution is to add android:thumbOffset value as suitable for your layout rather than setting it as half of your thumb icon width. If you set android:thumbOffset as +ve value (e.g 10dp) your thumb moves towards left side and this value is -ve (e.g -10dp) than your thumb moves towards right side.




回答4:


try giving the thumbOffset in the seekbar a negative value (like -3px).




回答5:


Add:

android:progress="0"

for seekbar in xml file.

OR

in Java activity file:

SeekBar sb = (SeekBar) v.findViewById(R.id.bright_seekbar);
sb.setMax(100);
sb.setProgress(0); // Set it to zero so it will start at the left-most edge

This should solve your problem.




回答6:


Set thumbOffset of your seekbar equal to double of thumb width. If width of your thumb is 8dp, then set android:thumbOffset equal to 16dp. It worked in my case.



来源:https://stackoverflow.com/questions/18995680/seekbar-thumb-center-not-at-the-start-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!