在Android中使用Google Material库创建垂直滑块。

huangapple go评论30阅读模式
英文:

Vertical slider in android using google material library

问题

我有一个在水平方向对齐的Google材料滑块:

<com.google.android.material.slider.Slider
    android:id="@+id/slider_tilt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tilt_btn"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginHorizontal="65dp"
    android:valueFrom="-4"
    android:valueTo="4"
    android:stepSize="0.5"/>

但我想要它在垂直方向并且对齐到屏幕的左侧。我尝试过旋转它,但无法获得左侧对齐,请告诉我是否有办法解决。

英文:

I have this google material slider aligned in horizontal direction:

<com.google.android.material.slider.Slider
    android:id="@+id/slider_tilt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tilt_btn"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginHorizontal="65dp"
    android:valueFrom="-4"
    android:valueTo="4"
    android:stepSize="0.5"/>

but I want it to be in vertical direction and aligned to the left of the screen. I tried rotating it but then couldn't get left side alignment, please let me know if there is a way out.

答案1

得分: 9

将属性 android:rotation 的值设置为 270,然后将其放入一个 FrameLayout 中。

英文:

        <FrameLayout
            android:layout_width="32dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:orientation="vertical">

            <com.google.android.material.slider.Slider
                android:id="@+id/sldPenWidth"
                android:layout_width="200dp"
                android:layout_height="32dp"
                android:layout_gravity="center"
                android:rotation="270"
                android:value="1"
                android:valueFrom="0.1"
                android:valueTo="10" />

        </FrameLayout>

set attr android:rotation value to 270, then put it into a FrameLayout

答案2

得分: 1

以下是您要翻译的内容:

目前我有这样的代码

    class VerticalSlider @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
        : com.google.android.material.slider.Slider(context, attrs, defStyleAttr) {
        init {
            rotation = 270f
        }
    
        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            super.onMeasure(heightMeasureSpec, widthMeasureSpec)
        }
    }

但我还需要包装它

```xml
    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <....VerticalSlider
            style="@style/InstSliderWhiteMatchWrap"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:value="100"
            app:haloRadius="15dp"
            app:thumbRadius="9dp" />

    </FrameLayout>

不过,我遇到了 haloRadius 的问题,不得不将它设置为较低的值以避免显示硬线,似乎布局边距没有被正确考虑,我尝试了许多方法...不过这种方式对我来说更可读和可重用一些。

英文:

For now I have something like this:

class VerticalSlider @JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : com.google.android.material.slider.Slider(context, attrs, defStyleAttr) {
    init {
        rotation = 270f
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec)
    }
}

Still I have to wrap it:

   &lt;FrameLayout 
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;&gt;

       &lt;....VerticalSlider
            style=&quot;@style/InstSliderWhiteMatchWrap&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:layout_marginBottom=&quot;10dp&quot;
            android:value=&quot;100&quot;
            app:haloRadius=&quot;15dp&quot;
            app:thumbRadius=&quot;9dp&quot; /&gt;

    &lt;/FrameLayout&gt;

I had issue with haloRadius thou, had to set it to lower value to not show hard lines, looks like layout margin is not take correctly into account and I tried milliard of things... Otherwise little more readable and reusable this way for me.

huangapple
  • 本文由 发表于 2020年7月30日 17:26:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定