2012-09-18 72 views
0

的尺寸我要畫一個橫幅下面的自定義視圖,像挑連接PICK剩餘視圖中繪製自定義視圖

DaTTab旗幟下面我要繪製矩形查看我的XML

  1. main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/relative_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <include android:id="@+id/banner" layout="@layout/upper_border" /> 

<com.example.ui_1.Border_Portrait 
     android:id="@+id/custom_display_view1" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/banner" 
     /> 

Draw_Border.jav

class Border_Portrait extends View{ 
    private static final int DEFAULT_SIZE = 100; 

    int mViewHeight , mViewWidth ; 
    Paint mPaint ; 
    Canvas mCanvas ; 
    int margin ; 
    float [] mPoints ; 
    int mLogoHeight ; 
    int mLogoWidth ; 
    int dy,dx ; 

    public Border_Portrait(Context context) { 
     super(context); 

    } 
    public Border_Portrait(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     margin = 7 ; 
     mPaint = new Paint(); 
     mPaint.setColor(Color.rgb(0,188,226)); 
     mPaint.setStrokeWidth(3); 
     mLogoHeight = ScreenUtils.convertDIPToPixels(getContext(), 50 + margin); 
     mLogoWidth = ScreenUtils.convertDIPToPixels(getContext(), 66 + margin); 
     dy = ScreenUtils.convertDIPToPixels(getContext(),1) ; 
     dx= ScreenUtils.convertDIPToPixels(getContext(),1) ; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.onDraw(canvas); 
     canvas.save(); 

     mPoints = new float [] { 
        margin + mLogoWidth ,margin,//1 
        getWidth()-margin ,margin ,//1 
        getWidth()-margin ,margin ,//2  
        getWidth()-margin , getHeight()-margin ,//2 
        getWidth()-margin , getHeight()-margin , //3 
        margin , getHeight()-margin , //3 
        margin , getHeight() - margin , //4 
        margin , mLogoHeight + dy ,//4 
        margin , mLogoHeight + dy , // 5 
        margin + mLogoWidth + dx ,mLogoHeight + dy, //5 
        margin + mLogoWidth ,mLogoHeight + dy, //6 
        margin + mLogoWidth ,margin, //6 
        } ; 
     canvas.drawLines(mPoints, mPaint); 
     canvas.restore(); 
    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
     this.setMeasuredDimension(parentWidth, parentHeight); 
    } 

    private int calculateMeasure(int measureSpec) { 
     return 0 ; 
    } 

} 

我的問題是如何測量邊框的寬度在運行時我有指

THIS

等環節太多,但至今沒有運氣

+0

得到解決方案jUst –

+1

然後刪除您的問題,或者最好將它發佈爲答案並接受它。 –

回答

0

剛剛在main.xml中得到了答案add margin

<com.example.ui_1.Border_Portrait 
     android:id="@+id/custom_display_view1" 
     android:layout_marginTop="50dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     />