Android 安卓按钮和绘图区域

Android 安卓按钮和绘图区域,android,button,drawable,Android,Button,Drawable,我是Android编程的新手。 我的任务很简单:我想创建一个只有两个对象的屏幕: 动作按钮 用于绘制图像、文本、圆等的可绘制区域 有没有可能有一个有效的例子,或者至少有一个指南 我知道如何对视图进行子类化,并使用以下命令对其进行int绘制: MyView d = new MyView(this); setContentView(d); 但这会用MyView填充所有屏幕,并且按钮不可见。 一些建议?您需要定义一个布局文件,并指定按钮和可绘图区域的相对位置。 确保它们在布局宽度

我是Android编程的新手。 我的任务很简单:我想创建一个只有两个对象的屏幕:

  • 动作按钮
  • 用于绘制图像、文本、圆等的可绘制区域
有没有可能有一个有效的例子,或者至少有一个指南

我知道如何对视图进行子类化,并使用以下命令对其进行int绘制:

    MyView d = new MyView(this);
    setContentView(d);
但这会用MyView填充所有屏幕,并且按钮不可见。
一些建议?

您需要定义一个布局文件,并指定按钮和可绘图区域的相对位置。 确保它们在布局宽度或布局高度中均未指定为填充父项


将contentView设置为此布局文件

您需要定义一个布局文件,并指定按钮和可绘图区域的相对位置。 确保它们在布局宽度或布局高度中均未指定为填充父项

将contentView设置为此布局文件,以了解详细信息

当你在eclipse中创建一个android项目时,你会发现res/layout/main.xml,这就是你的默认UI的定义,它是使用setContentView(R.layout.main)设置的;在onCreate方法中

要放置图像,可以对xml文本使用imageview和textview。类似于许多用于编辑文本、按钮等的小部件。一个简单的示例包括Imageview、textview和Button:

     <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

  <TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="HELLO ANDROID"
 android:layout_gravity="center"
 />

    <Button 
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:text="Click Me"
 />

您需要仔细了解细节

当你在eclipse中创建一个android项目时,你会发现res/layout/main.xml,这就是你的默认UI的定义,它是使用setContentView(R.layout.main)设置的;在onCreate方法中

要放置图像,可以对xml文本使用imageview和textview。类似于许多用于编辑文本、按钮等的小部件。一个简单的示例包括Imageview、textview和Button:

     <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

  <TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="HELLO ANDROID"
 android:layout_gravity="center"
 />

    <Button 
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:text="Click Me"
 />


好的,谢谢,我已经试过这个解决方案了。但是在java代码中,如何在imageView1上绘制呢?要绘制,我需要一个画布。一旦我从参考资料中获得ImageView,我如何才能获得一个与之结合的画布来绘制它呢?这个链接可能会有所帮助:好的,谢谢,我已经尝试过这个解决方案了。但是在java代码中,如何在imageView1上绘制?要画进去,我需要一张画布。一旦我从参考资料中获得ImageView,我如何才能获得一个画布,并将其与之结合在一起进行绘制?好的,谢谢,我已经尝试过这个解决方案。但是在java代码中,如何在imageView1上绘制呢?要绘制,我需要一个画布。一旦我从参考资料中获得ImageView,我如何才能获得一个与之结合的画布来绘制它呢?这个链接可能会有所帮助:好的,谢谢,我已经尝试过这个解决方案了。但是在java代码中,如何在imageView1上绘制?要画进去,我需要一张画布。一旦我从参考资料中获得ImageView,我如何才能获得一个与之结合的画布来绘制它呢?避免使用绝对布局。它们已弃用,并且不是必需的。它们使代码难以维护。至于从IVIEVIEW获取画布,你可能想考虑使用画布而不是IVIEVIEW,然后在画布上绘制图像。您可以在画布上定位图像。避免使用绝对布局。它们已弃用,并且不是必需的。它们使代码难以维护。至于从IVIEVIEW获取画布,你可能想考虑使用画布而不是IVIEVIEW,然后在画布上绘制图像。您可以在画布上定位图像。