さまざまな基本形状 (円、四角形など) の「パレット」を持つアプリケーションを作成しようとしています。ユーザーはこれらのコピーをキャンバスにドラッグ アンド ドロップできます。私の形状はxmlファイルで定義されています。現時点では、それらをキャンバスに移動できますが、(元のビューを移動しているため) パレットから消えるため、ユーザーが形状をクリックしたときに形状の新しいコピーを作成する方法が必要です。キャンバスにドラッグします。解決策を探してみましたが、LayoutInflaters を調べましたが、動作させることができませんでした。LayoutInflaters は進むべき道ですか、それともより良い解決策がありますか? 以下のコード スニペットを参照してください。
ありがとう
私の onDrag() で:
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
//owner.removeView(view);
LinearLayout container = (LinearLayout) v;
View copyview = getLayoutInflater().inflate(R.drawable.rectangle, null);
container.addView(copyview);
copyview.setVisibility(View.VISIBLE);
break;
編集:
長方形の XML:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<solid android:color="#ffff00ff" />
</shape>
ログキャット:
10-18 19:59:12.461: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:12.741: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:12.961: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:13.101: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:13.372: D/android.widget.GridLayout(830): vertical constraints: y1 - y0 > 160, y2 - y1 > 1038, y2 - y0 < 1038 are inconsistent; permanently removing: y2 - y0 < 1038.
10-18 19:59:13.481: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:13.542: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:13.661: D/gralloc_goldfish(830): Emulator without GPU emulation detected.
10-18 19:59:15.531: D/AndroidRuntime(830): Shutting down VM
10-18 19:59:15.531: W/dalvikvm(830): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-18 19:59:15.631: E/AndroidRuntime(830): FATAL EXCEPTION: main
10-18 19:59:15.631: E/AndroidRuntime(830): android.view.InflateException: Binary XML file line #2: Error inflating class shape
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-18 19:59:15.631: E/AndroidRuntime(830): at com.neonwizard.shapes.DragActivity$MyDragListener.onDrag(DragActivity.java:68)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.View.dispatchDragEvent(View.java:13461)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1155)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.os.Looper.loop(Looper.java:137)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-18 19:59:15.631: E/AndroidRuntime(830): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 19:59:15.631: E/AndroidRuntime(830): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 19:59:15.631: E/AndroidRuntime(830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-18 19:59:15.631: E/AndroidRuntime(830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-18 19:59:15.631: E/AndroidRuntime(830): at dalvik.system.NativeStart.main(Native Method)
10-18 19:59:15.631: E/AndroidRuntime(830): Caused by: java.lang.ClassNotFoundException: android.view.shape
10-18 19:59:15.631: E/AndroidRuntime(830): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
10-18 19:59:15.631: E/AndroidRuntime(830): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-18 19:59:15.631: E/AndroidRuntime(830): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
10-18 19:59:15.631: E/AndroidRuntime(830): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
10-18 19:59:15.631: E/AndroidRuntime(830): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
10-18 19:59:15.631: E/AndroidRuntime(830): ... 20 more
10-18 19:59:16.251: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:16.282: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'