我一直在SeekBar周围使用这个基本的包装,但发现它隐藏了拇指,或做了一些时髦的东西,比如在白色背景上使它变成白色,在Marshmallow下面.
我使用AS"BlankActivity"向导来创建一个项目来说明这一点,除了默认设置之外什么都没有改变.左边是Lollipop,同样的代码在Marshmallow右边运行:
有一个自定义水平SeekBar来测试是否存在定制它们的一般问题,但没有.左边的第一个垂直方向没有样式,这在Marshmallow之前很好,但是没有其他方式,中央显式使用Widget.Material.Light.SeekBar样式来测试默认情况是否未被拾取,并且最后一个给出了一个很大的线索,因为它使用旧的Widget.Holo.SeekBar样式然后出现,虽然看起来它几年前出现.
这是这个的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<com.otamate.seekbarmarshbug.CustomSeekBar
android:id="@+id/seekBarCustom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.otamate.seekbarmarshbug.VerticalSeekBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/seekBarCustom"
/>
<com.otamate.seekbarmarshbug.VerticalSeekBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_below="@+id/seekBarCustom"
android:layout_centerHorizontal="true"
style="@android:style/Widget.Material.Light.SeekBar"
/>
<com.otamate.seekbarmarshbug.VerticalSeekBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/seekBarCustom"
style="@android:style/Widget.Holo.SeekBar"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
CustomSeekBar:
package com.otamate.seekbarmarshbug;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.SeekBar;
public class CustomSeekBar extends SeekBar {
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Run Code Online (Sandbox Code Playgroud)
和VerticalSeekBar:
package com.otamate.seekbarmarshbug;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBar extends SeekBar {
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
试试这个代码.它工作正常!
<seekBar>
.....
android:splitTrack="false"
</seekBar>
Run Code Online (Sandbox Code Playgroud)
似乎棉花糖OS在某种程度上打破了画布上的平局.我实现了一个简单的修复.
我在自定义VerticalSeekBar的两个方法中进行了修改
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
drawThumb(c); //redrawing thumb
super.onDraw(c);
}
void drawThumb(Canvas canvas) {
Drawable thumb = getThumb();
if (thumb != null) {
Rect thumbBounds = thumb.getBounds();
canvas.save();
canvas.rotate(90, thumbBounds.exactCenterX(), thumbBounds.exactCenterY());
thumb.draw(canvas);
canvas.restore();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这只适用于Marshmallow设备.如果您尝试在前M的设备上使用此功能,则会显示两个拇指.如果您希望此功能适用于所有设备,则需要使用拇指的自定义属性完全重绘拇指.
检查此github仓库以获取与M https://github.com/3drobotics/AndroidWidgets一起使用的VerticalSeekBar
小智 -1
只需使用普通的 SeekBar 并使用 android:rotation="270" 这对我有帮助。
<SeekBar
android:id="@+id/seekBar"
android:rotation="270"
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3269 次 |
| 最近记录: |