如何在Android中制作垂直SeekBar?

Chr*_*ris 68 android seekbar

搜索栏可以垂直吗?我不是很擅长UI设计,所以如何让搜索栏更漂亮,请给我一些模板和示例.

Sat*_*urn 64

  1. 对于API 11及更高版本,可以使用seekbar的XML属性(android:rotation ="270")进行垂直效果.

    <SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:rotation="270"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对于较旧的API级别(例如API10),仅使用Selva的答案:https:
    //github.com/AndroSelva/Vertical-SeekBar-Android

  • 解决方案1 ​​ - 旋转的SeekBar无法正常工作.拇指被拉开,大小和位置无法正确设置(至少在RelativeLayout内部). (14认同)
  • 啊,我太快投了你.这很难奏效.滑块内部使用旋转前的宽度/高度,因此一切都是笨拙的位置和大小. (4认同)
  • 它确实有效,但你必须记住,尺寸会被交换,所以过去的宽度会变成高度等等.因此,当您增加高度时,实际上是拉伸它,这就是拇指被拉开的原因,如果您拉伸宽度,即使使用常规搜索条也会发生这种情况.旋转很烦人,所以我认为将搜索栏放在布局中并正确调整大小然后旋转布局并调整其大小以适合您的主布局会更容易. (2认同)
  • 这是非常烦人的:您使用 width 属性设置了 SeekBar 的实际高度,但在布局的其余部分中,这仍然是 SeekBar 元素的宽度。如果您随后为外部元素的 width 属性指定较小的值(因为垂直元素只需要较小的宽度),则 SeekBar 的垂直线将根据该宽度而缩短。这可以通过引入一个额外的包装器来解决,该包装器在具有小宽度属性的外部容器内仍然具有大宽度属性。但拇指确实还是有偏移。我放弃了:-p (2认同)

And*_*lva 48

这是一个非常好的垂直搜索栏实现.看一看.

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

这是我自己基于此的 Vertical and Inverted 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 嘿.我已经尝试了你发布的代码为.zip并且效果很好..除了一个问题:SeekBar仍然从左到右而不是从下到上绘制进度.没找到它编码的地方.你有什么建议吗?谢谢 (2认同)
  • 您好!喜欢你的verticalScrollbar倒置,我试图创建一个,但很难找到正确的旋转和翻译.我发现有一个小错误:如果MAX不是100,则无法正确计算进度.要修复它,你必须改变(100-i)到(getMax() - i).这种方式适用于任何范围. (2认同)

Xar*_*mer 22

工作实例

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在那里,粘贴代码并保存.现在在XML布局中使用它:

<android.widget.VerticalSeekBar
  android:id="@+id/seekBar1"
  android:layout_width="wrap_content"
  android:layout_height="200dp"
  />
Run Code Online (Sandbox Code Playgroud)

确保创建一个包android.widgetVerticalSeekBar.java在此包下创建


小智 13

尝试:

<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" > 

<SeekBar 
    android:id="@+id/seekBar1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:rotation="270" 
    /> 

</RelativeLayout> 
Run Code Online (Sandbox Code Playgroud)

  • 设置旋转会旋转滑块但不会旋转布局.layout_width仍然控制滑块的长度.因此,布局仍然认为它是宽度而不是高度.这使得正确插入设计变得有趣.我还没弄明白如何在GridLayout上放置一个旋转的搜索栏. (6认同)

Jör*_*eld 10

我使用了Selva的解决方案但有两种问题:

  • OnSeekbarChangeListener无法正常工作
  • 以编程方式设置进度无法正常工作.

我解决了这两个问题.您可以在此处找到解决方案(在我自己的项目包中)

https://github.com/jeisfeld/Augendiagnose/blob/master/AugendiagnoseIdea/augendiagnoseLib/src/main/java/de/jeisfeld/augendiagnoselib/components/VerticalSeekBar.java


小智 9

我们使用android:rotation="270"以下方法制作了一个垂直的SeekBar :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/camera_sv_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/camera_lv_expose"  
        android:layout_width="32dp"
        android:layout_height="200dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/camera_tv_expose"
            android:layout_width="32dp"
            android:layout_height="20dp"
            android:textColor="#FFFFFF"
            android:textSize="15sp"
            android:gravity="center"/>

        <FrameLayout
            android:layout_width="32dp"
            android:layout_height="180dp"
            android:orientation="vertical">

            <SeekBar
                android:id="@+id/camera_sb_expose"
                android:layout_width="180dp"
                android:layout_height="32dp" 
                android:layout_gravity="center"
                android:rotation="270"/>

        </FrameLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/camera_tv_help"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:text="@string/camera_tv"
        android:textColor="#FFFFFF" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

相机曝光补偿的屏幕截图:

在此输入图像描述


小智 7

这对我有用,只需将其放入您想要的任何布局中.

<FrameLayout
    android:layout_width="32dp"
    android:layout_height="192dp">

    <SeekBar
        android:layout_width="192dp"
        android:layout_height="32dp"
        android:layout_gravity="center"
        android:rotation="270" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


use*_*622 6

请注意,在我看来,如果您更改宽度,拇指宽度不会正确更改.我没有花时间修正它,我只是为我的情况修好了.这就是我做的.无法弄清楚如何联系原始创作者.

public void setThumb(Drawable thumb) {
    if (thumb != null) {
        thumb.setCallback(this);

        // Assuming the thumb drawable is symmetric, set the thumb offset
        // such that the thumb will hang halfway off either edge of the
        // progress bar.
        //This was orginally divided by 2, seems you have to adjust here when you adjust width.
        mThumbOffset = (int)thumb.getIntrinsicHeight();
    }
Run Code Online (Sandbox Code Playgroud)


Abh*_*pta 5

将其包裹在 FrameLayout 中,这样就不会出现 Size 问题。

  <FrameLayout
                android:layout_width="@dimen/_20dp"
                android:layout_marginStart="@dimen/_15dp"
                android:layout_marginEnd="@dimen/_15dp"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <SeekBar
                    android:layout_width="150dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"
                    android:rotation="270" />
  </FrameLayout>
Run Code Online (Sandbox Code Playgroud)