如何在 Android 中制作垂直的 SeekBar?

新手上路,请多包涵

SeekBar 可以垂直吗?我不太擅长UI设计,如何才能使 SeekBar 更漂亮,请给我一些模板和例子。

原文由 Chris 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

这是一个很好的垂直搜索栏实现。看一看。

http://560b.sakura.ne.jp/android/VerticalSlidebarExample.zip

这是我自己基于 的垂直和倒置 Seekbar 的实现

https://github.com/AndroSelva/Vertical-SeekBar-Android

 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:
            int i=0;
            i=getMax() - (int) (getMax() * event.getY() / getHeight());
            setProgress(i);
            Log.i("Progress",getProgress()+"");
            onSizeChanged(getWidth(), getHeight(), 0, 0);
            break;

        case MotionEvent.ACTION_CANCEL:
            break;
    }
    return true;
}

原文由 Andro Selva 发布,翻译遵循 CC BY-SA 3.0 许可协议

简单的答案

不要在搜索栏内使用 android:rotation="270" ,而是在环绕它的 FrameLayout 内使用它。

  <FrameLayout
    android:background="@color/gray"
    android:layout_width="300dp"
    android:layout_height="5dp"
    android:layout_marginEnd="-126dp"
    android:rotation="270"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">
<SeekBar
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</FrameLayout>

为了让我的框架布局为 24dp 边距,我计算了宽度 -150dp + 24dp,因为框架布局首先水平绘制,然后垂直旋转。

原文由 Pavle37 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题