将按钮添加到视图
Posted
技术标签:
【中文标题】将按钮添加到视图【英文标题】:Add a button to View 【发布时间】:2013-05-12 04:36:29 【问题描述】:我正在绘制一个图表,然后为输入数据添加一些按钮/标签/文本框。但是主布局上的按钮没有显示出来。如何向视图添加按钮(因为我在视图上使用画布绘制图形)
这里是画布类
public class DrawCross extends View
Paint paint = new Paint();
public DrawCross(Context context)
super(context);
paint.setColor(Color.BLACK);
Button btn = new Button(context);
@Override
public void onDraw(Canvas canvas)
canvas.drawLine(250, 450, 250, 20, paint);
canvas.drawLine(245, 25, 250, 20, paint);
canvas.drawLine(250, 20, 255, 25, paint);
canvas.drawLine(20, 300, 400, 300, paint);
canvas.drawLine(395, 295, 400, 300, paint);
canvas.drawLine(400, 300, 395, 305, paint);
这是主类
public class MainActivity extends Activity
DrawCross drawView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawCross(this);
setContentView(drawView);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return true;
固定:
DrawCross 类:
public class DrawCross extends View
Paint paint = new Paint();
public DrawCross(Context context)
super(context);
@Override
public void onDraw(Canvas canvas)
canvas.drawLine(250, 450, 250, 20, paint);
canvas.drawLine(245, 25, 250, 20, paint);
canvas.drawLine(250, 20, 255, 25, paint);
canvas.drawLine(20, 300, 400, 300, paint);
canvas.drawLine(395, 295, 400, 300, paint);
canvas.drawLine(400, 300, 395, 305, paint);
主类:
public class MainActivity extends Activity
DrawCross drawView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return true;
activity_main 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<TextView
android:layout_
android:layout_
android:text="a" />
<EditText
android:layout_
android:layout_ />
<Button
android:layout_
android:layout_
android:text="draw" />
</LinearLayout>
<com.example.khibong.DrawCross
android:id="@+id/myView"
android:layout_
android:layout_ />
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.khibong"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.khibong.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
和日志猫:
05-12 12:56:16.831: E/Trace(1715): error opening trace file: No such file or directory (2)
05-12 12:56:16.841: D/AndroidRuntime(1715): Shutting down VM
05-12 12:56:16.841: W/dalvikvm(1715): threadid=1: thread exiting with uncaught exception (group=0xb3e28288)
05-12 12:56:16.841: E/AndroidRuntime(1715): FATAL EXCEPTION: main
05-12 12:56:16.841: E/AndroidRuntime(1715): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.khibong/com.example.khibong.MainActivity: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.khibong.DrawCross
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.os.Looper.loop(Looper.java:137)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-12 12:56:16.841: E/AndroidRuntime(1715): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:56:16.841: E/AndroidRuntime(1715): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 12:56:16.841: E/AndroidRuntime(1715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-12 12:56:16.841: E/AndroidRuntime(1715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-12 12:56:16.841: E/AndroidRuntime(1715): at dalvik.system.NativeStart.main(Native Method)
05-12 12:56:16.841: E/AndroidRuntime(1715): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.example.khibong.DrawCross
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.createView(LayoutInflater.java:596)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-12 12:56:16.841: E/AndroidRuntime(1715): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.Activity.setContentView(Activity.java:1867)
05-12 12:56:16.841: E/AndroidRuntime(1715): at com.example.khibong.MainActivity.onCreate(MainActivity.java:18)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.Activity.performCreate(Activity.java:5008)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-12 12:56:16.841: E/AndroidRuntime(1715): ... 11 more
05-12 12:56:16.841: E/AndroidRuntime(1715): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
05-12 12:56:16.841: E/AndroidRuntime(1715): at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-12 12:56:16.841: E/AndroidRuntime(1715): at java.lang.Class.getConstructor(Class.java:431)
05-12 12:56:16.841: E/AndroidRuntime(1715): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
05-12 12:56:16.841: E/AndroidRuntime(1715): ... 22 more
【问题讨论】:
@Triode 我已经尝试为 DrawCross 类扩展 RelativeLayout,但是 RelativeLayout 的 onDraw 不喜欢 View 的 onDraw。当我更改为RelativeLayout 时,我绘制的所有东西都会消失。如何解决? @Triode 我已经添加了 super.onDraw 但抽奖仍然没有出现。我已经在帖子上更新了我的固定代码。有什么问题吗?? 你想达到什么目的?你可以发布你想要通过这样做得到的最终结果图像吗? @Triode 我需要同时画线和按钮。类似于我上面更新的图片 查看编辑后的答案。您可以在 xml 文件本身中添加更多选项。 【参考方案1】:我尝试了您早期的 DrawCross,它可以满足您的需求,但它有点难看。您必须设置基线以使其看起来不错
public class MainActivity extends Activity
DrawCross drawView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawCross drawCross = new DrawCross(this);
LinearLayout layout1 = (LinearLayout) findViewById(R.id.drawcross);
layout1.addView(drawCross);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return true;
XML
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_
android:layout_
android:weightSum="10"
android:orientation="vertical" >
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview1"
android:layout_
android:layout_
android:text="label1" />
<EditText
android:layout_
android:layout_
android:layout_alignBaseline="@id/textview1" />
<Button
android:layout_
android:layout_
android:text="Draw" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_
android:layout_
android:text="label2" />
<EditText
android:layout_
android:layout_ />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_
android:layout_
android:text="label3" />
<EditText
android:layout_
android:layout_ />
</LinearLayout>
<LinearLayout
android:id="@+id/drawcross"
android:layout_
android:layout_
android:layout_weight="7"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
【讨论】:
很好,明确的答案。真的很感激。甚至很难搜索到这个答案 - 一个包含以编程方式绘制的元素和布局元素的混合视图,并且您非常简洁明了地解释了答案。【参考方案2】:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@android:color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal" >
<TextView
android:layout_
android:layout_
android:text="a" />
<EditText
android:layout_
android:layout_ />
<Button
android:layout_
android:layout_
android:text="draw" />
</LinearLayout>
<yourPacakageName.DrawCross
android:id="@+id/myView"
android:layout_
android:layout_ />
</LinearLayout>
创建一个这样的布局并将其设置为您的活动的内容。还有
public class DrawCross extends View
使用 View 扩展您的 DrawCross
。 yourPackakageName 应该是你的 DrawCross
的包名。
<activity android:name="com.example.khibong.MainActivity"
把这个改成
<activity android:name=".MainActivity"
【讨论】:
以上是关于将按钮添加到视图的主要内容,如果未能解决你的问题,请参考以下文章