20

我有一个GraphicsView从该类扩展的View类,我想将这个GraphicsView类添加到我项目的主布局中。我怎样才能做到这一点?

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

<?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>
4

7 回答 7

27

您需要提供扩展 View 的类的完整路径,

<com.blah.blah.GraphicsView
         android:id="@+id/view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
于 2012-05-02T09:04:57.267 回答
17

如果我没记错的话,您需要提供更多构造函数来使用 xml 文件中的视图(将其添加到 xml 文件中,如“我和我们”建议的)。

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);
}

更新:固定代码。

于 2012-05-02T09:44:51.943 回答
8

我终于明白了,这里是 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"/>

和班级

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);
        }
}
于 2012-05-03T06:12:23.177 回答
6

你需要这样做:

LinearLayout v = (LinearView) findViewById(R.id.linnnnlayout);
GraphicsView myView = new myGraphicsView(this);
v.addView(myView);
于 2012-05-02T09:04:02.477 回答
6

因为您的自定义是java 编译器View中的内部类,所以会输出该类的名称。由于字符的原因,您不能直接将该名称用作 xml 布局中的名称,但您可以这样做:ActivityActivityName$GraphicsViewView$

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

where是声明您的类ActivityName的活动的名称。GraphicsView

于 2012-05-02T09:39:27.550 回答
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>
于 2012-05-02T09:47:25.837 回答
0

这对我有用,并在带有完整路径的 XML 中添加视图,并尝试为高度和宽度提供 wrapcontent。

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);
    }
}
于 2017-07-14T05:07:21.853 回答