2014-05-02 44 views
0

我想在佈局中使用一些自定義視圖,如圖片所示。如何獲得獨立視圖的畫布

enter image description here

紅色矩形,它只是來看邊界。裏面可能有不同的東西。

對於我創建的自定義視圖:

公共類CustomView擴展視圖{

public CustomView(Context context) { 
    super(context); 
} 

public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Paint paint = new Paint(); 
    paint.setColor(Color.WHITE); 
    paint.setStyle(Paint.Style.FILL); 
    canvas.drawCircle(5, 5, 5, paint); 
    canvas.save(); 
} 

}

中的onCreate

,然後某處()方法,我的一些看法添加到佈局:

layout.addView(new CustomView(this)); 
layout.addView(new CustomView(this)); 

由於結果圓是繪製在佈局的左上方(黑色直腸在圖片上)。

我以爲onDraw()方法包含單獨視圖的Canvas。

如何在單獨視圖的範圍內繪製某些東西,但不是所有的佈局?

回答

0

你可以在XML中做到這一點。您可以創建一個可繪製資源(稱爲red_border.xml)...

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <stroke android:width="1dp" android:color="@android:color/holo_red_light"/> 
      <size android:width="25dp" android:height="25dp"/> 
     </shape> 
    </item> 
</selector> 

並將其分配給特定視圖。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/red_border"> 
</LinearLayout> 

這將工作更多的意見,不只是佈局(TextView,EditText,等等...)。

希望這會有所幫助。

+0

剛剛意識到你可能一直在談論如何使用畫布來做到這一點。但是,這應該能夠給你看你想要的。 – Andrew