Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将自定义视图添加到布局中?_Android_Android Layout_Android View - Fatal编程技术网

Android 如何将自定义视图添加到布局中?

Android 如何将自定义视图添加到布局中?,android,android-layout,android-view,Android,Android Layout,Android View,我有一个GraphicsView类,它扩展自视图类,我想将这个GraphicsView类添加到我项目的主布局中。我该怎么做 static public class GraphicsView extends View { public GraphicsView(Context context) { super(context); } @Override protected void onDraw(Canvas canv

我有一个
GraphicsView
类,它扩展自
视图
类,我想将这个
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);
        }
    }
和my
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>

您需要执行以下操作:

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

您需要给出扩展视图的类的完整路径

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

因为自定义的
视图是
活动中的一个内部类
,java编译器将为该类输出名称
活动名称$GraphicsView
。由于
$
字符的原因,您不能将该名称直接用作xml布局中的
视图
名称,但您可以这样做:

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


其中,
ActivityName
是声明
GraphicsView
类的活动的名称。

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

更新:固定代码

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:



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

这对我来说是可行的,用完整路径添加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);
    }
}

我必须把这个放在哪里?这给了我一个例外!GraphicsView类在activity类中为GraphicsView类创建一个单独的类。它仍然给我带来异常这里是代码iam将此代码放在main.xml中对吗?我的包名是package gen.customfonts.namespace;那么,使用我之前发送给您的视图是否正确?谢谢你的帮助,我已经把这门课分开归档了。但是我有一个例外,说错误膨胀类com.gen.customfonts.namespace.GraphicsView我认为我在类名上有问题!!包装名称为package gen.customfonts.namespace;在xml中,我该如何称呼该类?@AnasBakez您是否尝试过将
GraphicsView
类保留在活动中,并使用我答案中的代码?你也应该实现视图超类的其他2个构造函数。我已经试过了,但它没有做任何事情!GraphicsView类中的视图不可见,但它没有给出异常,我认为类名称iam putting是错误的。包装名称为package gen.customfonts.namespace;活动名称是CustomFontsActivity类名应该是什么?@AnasBakez Try:
class=“gen.customfonts.namespace.CustomFontsActivity$GraphicsView”
@AnasBakez我想我找到了问题所在。将上面的代码与
属性一起使用,但不要使用
谢谢,这非常有帮助。当我使用单个构造函数时,我会出错,但在使用了所有这三个构造函数之后,我不会出错!
<TextView 
    android:id="@+id/Customfont"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />


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