Java 在布局中显示自定义视图
我目前有一个名为clientActivity的类,它包含一个绘制到画布上的finger paintig活动,该类有一个名为MyView的视图扩展。我想使画布在我的xml聊天布局中以相对布局显示 这是ClientActivity类Java 在布局中显示自定义视图,java,android,Java,Android,我目前有一个名为clientActivity的类,它包含一个绘制到画布上的finger paintig活动,该类有一个名为MyView的视图扩展。我想使画布在我的xml聊天布局中以相对布局显示 这是ClientActivity类 public class ClientActivity extends GraphicsActivity implements ColorPickerDialog.OnColorChangedListener { @Override protected void on
public class ClientActivity extends GraphicsActivity
implements ColorPickerDialog.OnColorChangedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
private Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
public void colorChanged(int color) {
mPaint.setColor(color);
}
public class MyView extends View {
ClientNetwork net = new ClientNetwork();
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
Thread fred = new Thread(net);
fred.start();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
我的chat.xml如下所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:weightSum="1">
<RelativeLayout android:id="@+id/relativeLayout3"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0.83">
<com.DrawTastic.ClientActivity android:id="@+id/MyView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/>
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="152dp" android:layout_weight="0.17">
</ListView>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<EditText
android:layout_width="232dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:id="@+id/input">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:layout_alignParentRight="true"
android:text="Send"
android:onClick="sent" android:id="@+id/send"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
您需要
您需要在布局chat.xml
中这样声明您的自定义视图:
//Notice that i've added .MyView
<com.DrawTastic.ClientActivity.MyView android:id="@+id/MyView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/>
编辑:
关于您遇到的问题,您应该在清单文件中声明您的活动,如下所示:
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs ) {
super(context, attrs);
}
<activity android:name=".YourActivity" />
您实际上可以这样做:public MyView(Context Context,AttributeSet attrs,int defStyle){super(Context,attrs,defStyle);}public MyView(Context Context,AttributeSet attrs){this(Context,attrs,0);}public MyView(Context Context Context Context){this(Context,null);}
您可以编辑我的答案lol;),我认为在你的评论中,我在回答中所说的是正确的,(我没有用conexte添加构造函数,只是因为他在代码中已经有了它:)干杯,我仍然收到大量错误,03-22 15:11:52.210:ERROR/AndroidRuntime(1124):java.lang.RuntimeException:无法启动活动。该活动位于清单中,因此不是这样。我只是有点困惑:/你的答案和我的评论之间的区别是,除了参数最多的构造函数外,我在所有构造函数中都调用this(),而不是在构造函数中调用super()。同样的,区别是你调用this,它将在构造函数中调用super(),并且每次调用this(…)使用其他参数的默认值:)编辑问题并添加异常的完整堆栈跟踪
<activity android:name=".YourActivity" />