繁体   English   中英

在整个父级中平均分配android垂直SeekBars

[英]Distributing android vertical SeekBars evenly across parent

我用下面的代码创建了一个“标准”垂直SeekBar(在SO上多次发布)。

package com.plainfieldworks.nofiseq;

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

public class VerticalSeekBar extends SeekBar{

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }   

    public VerticalSeekBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @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;
    }   

}

现在,我想使用其中的6个。 如果我只是使用RelativeLayout将它们彼此相邻放置,那么一切都很好(无法发布图片,缺乏声誉...)。

但是,我希望它们在水平方向上均匀分布在整个视图中,如果我在布局XML中将它们添加为LinearLayout的子代,并将它们的权重设置为1,则progressDrawable(我认为是-“栏”)将填充整个视图宽度稍加填充,拇指将放在左侧。

现在,我明白了为什么会发生这种情况,但是我不知道如何解决。 即我如何保持默认比例,并仍将SeekBars在整个视图中均匀地分布在每个视图中,以progressDrawable和thumb为中心?

一般来说,我是android和Java开发的新手,所以请耐心等待。 我希望我能解释这个问题。

使我胖乎乎的SeekBars的XML布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/cream"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".EditInst"
    android:padding="2dp">

    <LinearLayout android:id="@+id/header"
        android:background="#ffffbc40"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dp">

        <TextView android:id="@+id/instTxt"
            android:textIsSelectable="false"
            android:textColor="#ff000000"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"/>         

    </LinearLayout>
    <LinearLayout android:id="@+id/SeekBars"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/header">  

        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f0SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f1SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f2SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/cSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/sSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
        <com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/dSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>

    </LinearLayout>

</RelativeLayout>

最直接的方法可能只是在两者之间添加另一个层。 您可以使用诸如当前当前SeekBar所在的FrameLayout类的东西,这样您就可以拥有“胖的FrameLayout ”,然后在每个FrameLayout放置一个SeekBar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM