0

我有这个我正在尝试实现的视图,它基本上是一个可以围绕水平轨道移动的拇指。

当它在没有任何特殊布局参数(例如 layout_below)的 RelativeLayout 中时,它可以很好地绘制,但是一旦我在其上放置了 layout_below 参数,它就根本不会绘制!

这可能是什么原因?

公共类 TrackSlider 扩展视图 {

/**
 * The thumb of the slider will be limited to move so that it's center does
 * not go beyond the width of the track.
 */
public static final int THUMB_ANCHOR_CENTER = 0;

/**
 * The thumb of the slider will be limited to move so that it's edges do not
 * go beyond the width of the track.
 */
public static final int THUMB_ANCHOR_EDGES = 1;

private float mSliderPosition;
private float mSnapPosition = 0f;
private boolean mSnapEnabled = false;
private int mThumbAnchor;

private Drawable mThumbDrawable;
private Drawable mTrackDrawable;
private int mTrackWidth;
private int mSlideRange;
private int mThumbWidth;
private int mThumbXOffset;

public TrackSlider(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = getContext().obtainStyledAttributes(attrs,
            styleable.TrackSlider);
    final int N = a.getIndexCount();
    for (int i = 0; i < N; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
        case styleable.TrackSlider_snapEnabled:
            setSnapEnabled(a.getBoolean(i, false));
            break;
        case styleable.TrackSlider_snapPosition:
            setSnapPosition(a.getFloat(i, 0.0f));
            break;
        case styleable.TrackSlider_startPosition:
            setThumbPosition(a.getFloat(i, 0.0f));
            break;
        case styleable.TrackSlider_thumb:
            setThumbDrawable(a.getDrawable(i));
            break;
        case styleable.TrackSlider_track:
            setTrackDrawable(a.getDrawable(i));
            break;
        case styleable.TrackSlider_thumbAnchor:
            mThumbAnchor = 0;//a.getInt(i, THUMB_ANCHOR_CENTER);
            break;
        }
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measuredWidth = 0, measuredHeight = 0;
    switch (MeasureSpec.getMode(widthMeasureSpec)) {
    case MeasureSpec.EXACTLY:
        measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        break;
    case MeasureSpec.AT_MOST:
        measuredWidth = Math.min(
                MeasureSpec.getSize(widthMeasureSpec),
                Math.max(mTrackDrawable.getIntrinsicWidth(),
                        mThumbDrawable.getIntrinsicWidth()));
        break;
    case MeasureSpec.UNSPECIFIED:
        measuredWidth = Math.max(mTrackDrawable.getIntrinsicWidth(),
                mThumbDrawable.getIntrinsicWidth());
        break;
    }
    switch (MeasureSpec.getMode(heightMeasureSpec)) {
    case MeasureSpec.AT_MOST:
        measuredHeight = Math.min(MeasureSpec.getSize(heightMeasureSpec),
                Math.max(mTrackDrawable.getIntrinsicHeight(),
                        mThumbDrawable.getIntrinsicHeight()));
        break;
    case MeasureSpec.EXACTLY:
        measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
        break;
    case MeasureSpec.UNSPECIFIED:
        measuredHeight = Math.max(mTrackDrawable.getIntrinsicHeight(),
                mThumbDrawable.getIntrinsicHeight());
        break;
    }
    setMeasuredDimension(measuredWidth, measuredHeight);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right,
        int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    int w = right - left;
    if (mThumbAnchor == THUMB_ANCHOR_CENTER) {
        mTrackWidth = w - getPaddingRight() - getPaddingLeft()
                - mThumbWidth;
    } else {
        mTrackWidth = w - getPaddingRight() - getPaddingLeft();
    }
    mThumbXOffset = getPaddingLeft();
    mSlideRange = w - getPaddingRight() - getPaddingLeft()
            - mThumbWidth;
    mTrackDrawable.setBounds((w - mTrackWidth) / 2, top + getPaddingTop(), (w + mTrackWidth) / 2, getBottom() - getPaddingBottom());
    mThumbDrawable.setBounds(mThumbXOffset,
            getPaddingTop() + getTop(), mThumbXOffset + mThumbWidth,
            getBottom() - getPaddingBottom());
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int pos = (int) (mSliderPosition * mSlideRange);
    Rect r = new Rect();
    mThumbDrawable.copyBounds(r);
    r.offset(pos, 0);
    mThumbDrawable.setBounds(r);
    mTrackDrawable.draw(canvas);
    mThumbDrawable.draw(canvas);
    r.offset(-pos, 0);
    mThumbDrawable.setBounds(r);
}

    /* getters and setters... */

}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:twoday="http://schemas.android.com/apk/res/com...."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="normal" />

    <Button
        android:id="@+id/btnContinue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/message"
        android:layout_marginRight="10dp"
        android:background="@drawable/btn_dialog_continue" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btnContinue"
        android:layout_toLeftOf="@id/btnContinue"
        android:background="@drawable/btn_dialog_cancel" />

    <com.twoday.gallerys.views.TrackSlider
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnCancel"
        twoday:snapEnabled="true"
        twoday:startPosition="0.5"
        twoday:thumb="@drawable/confirm_slider_thumb"
        twoday:thumbAnchor="center"
        twoday:track="@drawable/confirm_slider_track" />

</RelativeLayout>
4

1 回答 1

0

好的,我找到了解决该问题的方法 - 将视图包装在线性布局中,然后将 layout_below 属性应用于布局而不是视图本身。

于 2012-04-18T09:47:23.450 回答