2011-11-23 38 views
0

我創建一個視圖,視圖代碼,如何在佈局中使用我自己的視圖?

public class MyDraw extends View{ 



    //List<Point> mArryLstpoints = new ArrayList<Point>(); 
    Paint paint =new Paint(); 
    Paint mPaintAlphabet = new Paint(); 
    public MyDraw(Context context) { 
     super(context); 


     paint.setColor(Color.BLUE); 
     paint.setAntiAlias(true); 


     // TODO Auto-generated constructor stub 
    } 
    public MyDraw(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 

     paint.setColor(Color.BLUE); 
     paint.setAntiAlias(true); 

     mPaintAlphabet.setDither(true); 
     mPaintAlphabet.setColor(0xFFFF0000); 
     mPaintAlphabet.setStyle(Paint.Style.STROKE); 
     mPaintAlphabet.setStrokeJoin(Paint.Join.ROUND); 
     mPaintAlphabet.setStrokeCap(Paint.Cap.ROUND); 
     mPaintAlphabet.setStrokeWidth(3); 
     mPaintAlphabet.setTextSize(400); 
    } 

    public void onDraw(Canvas canvas){ 
     canvas.drawColor(Color.GRAY); 
     canvas.drawText("A",100,350, mPaintAlphabet); 
     System.out.println("in on draw"); 
     for(Point mPoint:MyAlphabetsActivity.mArryLstpoints) 
     canvas.drawCircle(mPoint.x, mPoint.y, 12, paint); 

    } 

    } 

我想利用在佈局這個看法,我想設置爲view.I背景圖片我用下面的代碼,

<?xml version="1.0" encoding="utf-8"?> 

    <com.qteq.myalphabets.MyDraw 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" ></com.qteq.myalphabets.MyDraw> 

我的活動類,

public class MyAlphabetsActivity extends Activity { 
    /** Called when the activity is first created. */ 

    MyDraw mMyDraw; 
    Button mBtnOk; 
    AttributeSet attributeSet; 
    public static List<Point> mArryLstpoints = new ArrayList<Point>(); 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     /* 
     * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     * WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     */ 
     /* 
     * mMyDraw=new MyDraw(this); setContentView(mMyDraw); 
     * mMyDraw.requestFocus(); 
     */ 
     mMyDraw = (MyDraw) findViewById(R.id.mMyDraw_layout); 
     mMyDraw.bringToFront(); 
     mMyDraw.requestFocus(); 
     mMyDraw.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       Point mPoint=new Point(); 
       mPoint.x=event.getX(); 
       mPoint.y=event.getY(); 
       System.out.println("in on touch"); 
       mArryLstpoints.add(mPoint); 
       System.out.println("Array list is-----"+mArryLstpoints); 

       setContentView(R.layout.main); 
       return true; 
      } 
     }); 
     // setContentView(R.layout.main); 
    } 
} 
    class Point{ 
     public float srtx; 
     float x, y; 

     @Override 
     public String toString() { 
      System.out.println("in on point"); 
      return x + ", " + y;  
    } 
    } 

和顯示和onTouch方法調用一次.....請幫我

+0

在onTouch方法中添加setContentView(R.layout.main)後,它只能工作一次,接下來不工作... – suresh

+0

您沒有發佈完整的代碼,因此很難說。碼。 – Delwin

回答

1

您正在使用fill_parent寬度和高度添加視圖,因此沒有空間來添加按鈕和圖像等其他視圖。

可能實施的一個

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <com.qteq.myalphabets.MyDraw 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_contentt" ></com.qteq.myalphabets.MyDraw> 
<Button/> 
<ImageView/> 
</LinearLayout> 

不要忘記在MyDraw.java

** * ** *添加構造

public MyDraw(Context context, AttributeSet attributeSet) { 
     super(context, ttributeSet); 

* * * 編輯 * * ** * **

onTouch() 回報super(v, event)代替true; 還檢查是否需要編寫​​來查看或畫布。

+0

嗨Rajawat感謝回覆,我已經包括上述代碼到我的應用程序....它顯示視圖...但視圖不工作....意味着我的視圖包含字母表「A」,如果我們畫出任何像「A」或「B」或「C」這樣的不工作的地方, – suresh

+0

可能會在畫布邊界150,450處出現縱座標。所以首先獲取高度和寬度bt Canvas.getHeight()..並在其邊界內繪製alhabet。第二個循環中的字母與字母重疊的可能性更大。 –

+0

字母顯示(非常小)...但繪圖不工作....意味着onTouch方法不打電話 – suresh

1

您可以將視圖放置在XML中的佈局中,並且在該佈局中還可以有其他元素。你也可以設置佈局背景。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="@drawable/mybackground"> 

<com.qteq.myalphabets.MyDraw 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

<Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 


</LinearLayout> 
+0

嗨亞歷克斯感謝重播..我用我的layout.it的代碼給佈局InflateException – suresh

+0

可能是因爲我已經拼寫包裝內容而不是wrap_content。我糾正了。爲什麼你想膨脹,你可能只是在你的活動中使用setContentView –

+0

public class MyActivity extends活動{ \t MyDraw mMyDraw; (保存實例狀態) \t \t \t setContentView(R.layout.main); \t} } – suresh

相關問題