英文:
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:
   <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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论