Android垂直搜索杆拇指对齐不正确

Android垂直搜索杆拇指对齐不正确,android,Android,我有一个带垂直搜索杆的线性布局。对于垂直搜索栏,我使用自定义类。使用android:layout_weight=1时,seekbar拇指对齐不正确 verticalseekbar.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="ma

我有一个带垂直搜索杆的线性布局。对于垂直搜索栏,我使用自定义类。使用android:layout_weight=1时,seekbar拇指对齐不正确

verticalseekbar.xml

<LinearLayout 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:orientation="horizontal" >

    <com.example.verticalseekbardemo.VerticalSeekBar
        android:id="@+id/seekBar1"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:progress="50" />

    <com.example.verticalseekbardemo.VerticalSeekBar
        android:id="@+id/seekBar2"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:progress="30" />

    <com.example.verticalseekbardemo.VerticalSeekBar
        android:id="@+id/seekBar3"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:progress="10" />

    <com.example.verticalseekbardemo.VerticalSeekBar
        android:id="@+id/seekBar4"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1" />

    <com.example.verticalseekbardemo.VerticalSeekBar
        android:id="@+id/seekBar5"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:progress="80" />

</LinearLayout>

VerticalSeekBar.java   

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
    public synchronized void setProgress(int progress) // it is necessary for
                                                        // calling setProgress
                                                        // on click of a button
    {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }

    @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;
    }
}
有人遇到过这样的问题吗


谢谢。

我也有同样的问题。解决此问题的唯一方法是将每个seekbar放入框架布局中,如下所示:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.example.verticalseekbardemo.VerticalSeekBar
              android:id="@+id/seekBar1"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:progress="50" />
 </FrameLayout>

我希望能帮助你。

我也有同样的问题。解决此问题的唯一方法是将每个seekbar放入框架布局中,如下所示:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.example.verticalseekbardemo.VerticalSeekBar
              android:id="@+id/seekBar1"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:progress="50" />
 </FrameLayout>
我希望能帮助你。

这里有一个例子

<LinearLayout
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:background="#ffffff">
 <com.example.yourpackgename_where_VerticalSeekbar_is_define.VerticalSeekBar
              android:id="@+id/first_seekbar"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:progress="50" />
</LinearLayout>
要添加5搜索条,我建议使用水平方向进行线性布局,权重和为5,然后在布局内部使用不同ID复制上述代码5次,这里是一个示例

<LinearLayout
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:background="#ffffff">
 <com.example.yourpackgename_where_VerticalSeekbar_is_define.VerticalSeekBar
              android:id="@+id/first_seekbar"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:progress="50" />
</LinearLayout>

要添加5搜索栏,我建议使用水平方向的线性布局,权重和为5,然后在布局内部使用不同ID复制上述代码5次,而不是使用垂直搜索栏添加android:rotation=90在正常搜索栏代码中。 例:-


不要使用垂直seekbar,而是在普通seekbar代码中添加android:rotation=90。 例:-


如果您想使用自定义VerticalSeekBar,我认为问题在于画布需要在中心旋转以获得所需的效果,尽管我可能错了

int centerX;
int centerY;

protected void onDraw(Canvas c) {
    c.translate(centerY, centerX);
    c.rotate(-90);

    super.onDraw(c);
}

@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
    super.onSizeChanged(width, height, oldw, oldh);

    centerX = width / 2;
    centerY = height / 2;
}

如果您想使用自定义VerticalSeekBar,我认为问题在于画布需要在中心旋转以获得所需的效果,尽管我可能错了

int centerX;
int centerY;

protected void onDraw(Canvas c) {
    c.translate(centerY, centerX);
    c.rotate(-90);

    super.onDraw(c);
}

@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
    super.onSizeChanged(width, height, oldw, oldh);

    centerX = width / 2;
    centerY = height / 2;
}

我有同样的问题,我通过添加

android:maxHeight="300dp"
在布局文件中查找kbar


希望这对您有所帮助。

我也有同样的问题,我通过添加

android:maxHeight="300dp"
在布局文件中查找kbar


希望这能对您有所帮助。

您所说的不正确是什么意思?请看一看图片。您打算进行哪种校准?Seekbar拇指校准。您设法做到了吗?您所说的不正确是什么意思?请看一看图片。您打算进行哪种校准?Seekbar拇指校准。您设法做到了吗?