gpt4 book ai didi

java - 在布局中显示自定义 View

转载 作者:行者123 更新时间:2023-12-01 15:28:49 24 4
gpt4 key购买 nike

我目前有一个名为 clientActivity 的类,其中包含在 Canvas 上绘制的手指绘画 Activity ,该类具有名为 MyView 的 View 扩展。我想让 Canvas 出现在我的 xml 聊天布局上的相对布局中。

这是 ClientActivity 类

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>

我只需要 Canvas 出现在其中一个布局内的 chat.xml 中,以便我可以在其下方放置一个 ListView 。

非常感谢任何帮助!

谢谢

编辑:堆栈跟踪。

03-22 15:32:21.310: ERROR/AndroidRuntime(1301): FATAL EXCEPTION: main
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DrawTastic/com.DrawTastic.GameActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.DrawTastic.ClientActivity.MyView
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.os.Looper.loop(Looper.java:123)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at java.lang.reflect.Method.invoke(Method.java:507)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at dalvik.system.NativeStart.main(Native Method)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.DrawTastic.ClientActivity.MyView
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.Activity.setContentView(Activity.java:1657)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at com.DrawTastic.GameActivity.onCreate(GameActivity.java:13)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): ... 11 more
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): Caused by: java.lang.ClassNotFoundException: com.DrawTastic.ClientActivity.MyView in loader dalvik.system.PathClassLoader[/data/app/com.DrawTastic-2.apk]
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): ... 21 more

最佳答案

您需要在布局 chat.xml 中声明您的自定义 View :

//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"/>

另一件事,你应该重写 MyView 类中 View 的其他构造函数,如下所示:

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


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

编辑:

关于您遇到的问题,您应该在 Manifest 文件中声明您的 Activity,如下所示:

<activity android:name=".YourActivity" />

关于java - 在布局中显示自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com