2012-04-17 14 views
0

私はこのビューを実装しようとしています。これは基本的に水平トラックの周りを移動可能な親指です。layout_belowパラメータを適用したときにAndroidビューが描画されない

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> 
+0

あなたはそれが画面の下から押し出されていないことは確かですか? – FoamyGuy

+0

ビューはダイアログに描画されており、ダイアログは画面全体を占めるわけではないので、layout_height attrをwrap_contentに設定したので、それが押し出されると奇妙になります... – saarraz1

+0

また、実際にはダイアログが実際に描かれているかのようにダイアログが大きくなりますが、そうではありません。 – saarraz1

答えて

0

OKを、私はこの問題の回避策を発見した - 適用その後のLinearLayoutのビューをラップlayout_below属性をビュー自体ではなくレイアウトに追加します。

関連する問題