在父级上均匀分布android垂直SeekBars

时间:2013-03-13 20:40:55

标签: android seekbar

我使用下面的代码创建了一个“标准”垂直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(我认为它是 - “bar”)将填充整个视图宽度只有一点点填充,拇指将放在左侧。

现在我明白为什么会发生这种情况,但我不知道如何解决这个问题。即如何保持默认比例并仍然将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>

1 个答案:

答案 0 :(得分:0)

最直接的方法可能是简单地在两者之间添加另一层。您可以使用当前SeekBars所在的FrameLayout之类的内容,以便您拥有“胖乎乎的FrameLayout”,然后在每个FrameLayout中放置一个SeekBar。