2012-05-02 5 views
10

Tôi có một lớp học GraphicsView kéo dài từ lớp View và tôi muốn thêm lớp này GraphicsView vào bố cục chính trong dự án của tôi. Làm thế nào tôi có thể làm điều đó?Cách thêm chế độ xem tùy chỉnh vào bố cục?

static public class GraphicsView extends View { 
     public GraphicsView(Context context) { 
     super(context); 
     } 
     @Override 
     protected void onDraw(Canvas canvas) { 
     // Drawing commands go here 
      Path rect = new Path(); 
      rect.addRect(100, 100, 250, 50, Direction.CW); 
      Paint cPaint = new Paint(); 
      cPaint.setColor(Color.LTGRAY); 
      canvas.drawPath(rect, cPaint); 
     } 
    } 

main.xml tôi:

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <TextView 
     android:id="@+id/Customfont" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <View 
     android:id="@+id/view" 
      android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
+1

View.addView (yourView); – mihail

+0

nơi tôi đã phải đặt này? – AnasBakez

Trả lời

17

Bạn cần phải cung cấp đường dẫn đầy đủ của lớp học của bạn kéo dài View,

<com.blah.blah.GraphicsView 
     android:id="@+id/view" 
      android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
+0

nó mang lại cho tôi ngoại lệ! lớp GraphicsView nằm trong lớp hoạt động – AnasBakez

+0

Tạo một lớp riêng biệt cho lớp GraphicsView. –

+2

nó vẫn cho tôi ngoại lệ ở đây là mã AnasBakez

4

bạn cần phải làm điều này:

LinearLayout v = (LinearView) findViewById(R.id.linnnnlayout); 
GraphicsView myView = new myGraphicsView(this); 
v.addView(myView); 
4

Vì custo của bạn m View là một lớp bên trong trong Activity trình biên dịch java của bạn sẽ xuất ra tên ActivityName$GraphicsView cho lớp đó. Bạn không thể sử dụng tên trực tiếp như tên View trong cách bố trí xml vì nhân vật $ nhưng bạn có thể làm điều đó như thế này:

<view 
    class="com.package.here.ActivityName$GraphicsView" 
    android:id="@+id/view" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

nơi ActivityName là tên của hoạt động nơi lớp GraphicsView của bạn được khai báo .

+0

tôi đã đặt lớp trong tập tin riêng biệt. nhưng iam có ngoại lệ mà nói lỗi lạm phát lớp com.gen.customfonts.namespace.GraphicsView tôi nghĩ rằng iam có vấn đề trong tên lớp !!tên pakage là gói gen.customfonts.namespace; những gì tôi nên gọi lớp trong xml? – AnasBakez

+0

@AnasBakez Bạn đã thử rời lớp 'GraphicsView' mà trước đó bạn đã có nó, trong hoạt động và sử dụng mã từ câu trả lời của tôi chưa? Ngoài ra, bạn nên thực hiện 2 hàm tạo khác của lớp siêu View. – Luksprog

+0

tôi đã thử nó nhưng nó không làm bất cứ điều gì! khung nhìn trong lớp GraphicsView không hiển thị, nhưng nó không đưa ra một ngoại lệ, tôi nghĩ rằng tên lớp iam put là lỗi. tên pakage là gói gen.customfonts.namespace; tên hoạt động là CustomFontsActivity tên lớp nên là gì? – AnasBakez

12

Nếu tôi nhớ chính xác, bạn cần phải cung cấp thêm hàm tạo để sử dụng chế độ xem từ tệp xml (thêm tệp đó vào tệp xml như đề xuất "Tôi và Chúng tôi").

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

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

public GraphicsView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

Cập nhật: mã cố định.

+0

Cảm ơn, điều này rất hữu ích. – SingularityFuture

0
public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    LinearLayout v = (LinearLayout) findViewById(R.id.linearLayout); 
    MyGraphics myView = new MyGraphics(this); 
    v.addView(myView); 
} 
} 


public class MyGraphics extends View { 
public MyGraphics(Context context) { 
    super(context); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    // Drawing commands go here 
    Path rect = new Path(); 
    rect.addRect(100, 100, 250, 50, Direction.CW); 
    Paint cPaint = new Paint(); 
    cPaint.setColor(Color.LTGRAY); 
    canvas.drawPath(rect, cPaint); 
} 


} 

XML:

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

<TextView 
    android:id="@+id/Customfont" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 


</LinearLayout> 
6

i finaly đã nhận nó ở đây là mã mã XML

<com.customfonts.namespace.BreakDownBar 
     android:id="@+id/gview" 
      android:layout_width="fill_parent" 
     android:layout_height="20dip" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:background="@color/BreakDownBarBack"/> 

và lớp

package com.customfonts.namespace; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Path.Direction; 
import android.util.AttributeSet; 
import android.view.View; 



public class BreakDownBar extends View { 

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

     @Override 
     protected void onDraw(Canvas canvas) { 
       //Draw rectangle; 
       Path rect = new Path(); 
       rect.addRect(0, 0,250, 150,Direction.CW); 
       Paint cpaint = new Paint(); 
       cpaint.setColor(Color.GREEN); 
       canvas.drawPath(rect, cpaint); 
     } 
} 
0

Điều này làm việc cho tôi và thêm chế độ xem trong XML với đường dẫn đầy đủ và thử đưa nội dung bọc cho chiều cao và chiều rộng.

public class RectangleView extends View { 
    public RectangleView(Context context) { 
     super(context); 
    } 

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

    public RectangleView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.GRAY); 
     canvas.drawColor(Color.BLUE); 
     canvas.drawRect(10,10,50,50, paint); 
    } 
}