Thứ Hai, 16 tháng 9, 2013

Fix SeekBar thumb centering

http://qtcstation.com/2011/05/android-how-to-fix-seekbar-bar-thumb-centering-issues/comment-page-1/#comment-3161


3

If you ever need to change the height of your Seekbar you may find that the thumb is not centering properly.
The fix for this is actually pretty simple. It just took me a while to find, so i'm posting this in the hopes of helping fellow developers cut down on their search time.
After trying things like gravity and margins and such with no success, i wondered well how the heck does android do it anyway?
That led me to seeing how they define their default seekbar style.
1<item name="android:indeterminateOnly">false</item>
2<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
3<item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
4<item name="android:minHeight">20dip</item>
5<item name="android:maxHeight">20dip</item>
6<item name="android:thumb">@android:drawable/seek_thumb</item>
7<item name="android:thumbOffset">8dip</item>
8<item name="android:focusable">true</item>
Which i then compared to mine.
1<item name="android:layout_width">fill_parent</item>
2<item name="android:layout_height">60dp</item>
3<item name="android:thumb">@drawable/slider_thumb</item>
4<item name="android:progressDrawable">@drawable/seekbar</item>
What immediately caught my eye was the minHeight and maxHeight attributes. It's possible that when this widget is being laid out android takes these values into account so I tried it.
1<item name="android:layout_width">fill_parent</item>
2<item name="android:layout_height">60dp</item>
3        <item name="android:minHeight">60dp</item>
4        <item name="android:maxHeight">60dp</item>
5<item name="android:thumb">@drawable/slider_thumb</item>
6<item name="android:progressDrawable">@drawable/seekbar</item>
Lo and behold it worked!
centered

Không có nhận xét nào:

Đăng nhận xét