Android编程-如何使用代码访问main.XML布局中的[绘制]XML视图

Android编程-如何使用代码访问main.XML布局中的[绘制]XML视图,android,xml,layout,view,drawing,Android,Xml,Layout,View,Drawing,好的,我是安卓系统编程的新手,在图形部分有点困难。了解在XML文件中创建布局的美妙之处,但却不知道如何访问各种元素,尤其是在其上绘制的视图元素 请参见此处我的布局示例main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" and

好的,我是安卓系统编程的新手,在图形部分有点困难。了解在
XML
文件中创建布局的美妙之处,但却不知道如何访问各种元素,尤其是在其上绘制的视图元素

请参见此处我的布局示例
main.xml

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

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"   
           android:layout_height="wrap_content">

           <TextView xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/Title" android:text="App title"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:textColor="#000000"
               android:background="#A0A0FF"/>
     </LinearLayout>

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/PaperLayout" 
          android:layout_width="fill_parent" 
          android:layout_height="0dp" 
          android:layout_weight="1" 
          android:orientation="horizontal" 
          android:focusable="true">

          <View 
              android:id="@+id/Paper" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" />
     </LinearLayout>


     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content">

          <Button 
               android:id="@+id/File" 
               android:layout_width="fill_parent"
               android:layout_weight="1" 
               android:layout_height="34dp"
               android:layout_alignParentTop="true"            
               android:layout_centerInParent="true"
               android:clickable="true" android:textSize="10sp" 
               android:text="File" />

         <Button 
              android:id="@+id/Edit" 
              android:layout_width="fill_parent"
              android:layout_weight="1" 
              android:layout_height="34dp"
              android:clickable="true" 
              android:textSize="10sp" android:text="Edit" />
     </LinearLayout>
</LinearLayout>
在我的主java文件中
MyApp.java
。我在
onCreate()之后添加了这个

但是我在CustomView部分出错; “默认构造函数的隐式超级构造函数
ImageView()
未定义。必须定义显式构造函数”

Eclipse提供了3个关于添加构造函数的快速修复方法,但没有任何帮助,它删除了错误,但在应用程序运行时给出了错误

我希望有人能帮我解决这个问题并提供一个解决方案,也许还能解释为什么我不能在
main.xml
layout文件中创建一个视图元素并在代码中加以利用

更新: 好的,我仍在尝试使用您的解决方案,这仍然给我带来问题和各种错误。但最重要的是,我仍然不明白为什么我必须创建一个自定义类来在我的
布局中使用,而我甚至必须选择使用wysiwyg布局编辑器来插入一个视图元素,并在我的主代码文件中“仅”绘制它。请有人回答我这个问题

不管怎样,现在我有了

package com.example.myapp;

import com.example.myapp.CustomView;
//and here comes other import

public class MyApp extends Activity { 
//rest of code including OnCreate()
我创建了一个新类,新文件CustomView.Java,在这里我直接粘贴了您的代码,使其看起来像

public class CustomView extends ImageView {
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         Paint paint = new Paint();
         paint.setARGB(255, 150, 205, 129);
         paint.setTextSize(20);
         canvas.drawText("Your Text", 1, 1, paint);
     }
}
好的,这个类代码希望我为
ImageView
Canvas
Paint
添加
import
,我这样做了,然后我又回到了错误的公共类
*CustomView*
部分; “默认构造函数的隐式超级构造函数ImageView()未定义。必须定义显式构造函数”


谢谢你帮我,谢谢

要在活动中使用main.xml中定义的视图,请重写活动的
onCreate
方法,例如:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
}


编辑:对不起,我在你的问题中将“在视图上绘制”误读为“绘制视图”:)请参见L0rdAli3n的答案。

重写onCreate方法,然后使用方法
findViewById
。例如,要使用和修改视图背景色,请使用以下命令:

View v = (View)findViewById(R.id.Paper);
v.setBackgroundColor(myColor);

同样,修改布局中定义的任何其他视图/小部件

以绘制视图,创建自己的自定义视图

为此,您需要添加一个新类。使用eclipse只需文件->新建->类,就可以了。覆盖onDraw()-方法以满足您的需要,并将CustomView添加到布局中:
public class CustomView extends ImageView {
    // The needed constructors:
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setARGB(255, 150, 205, 129);
paint.setTextSize(20);
    canvas.drawText("Your Text", xposition, yposition, paint);
}
}

在xml文件中,您可以像这样添加CustomView:



如果您不想在res文件夹中的drawable上绘图,请指定customView的“src”参数。

谢谢您的回复,不过我知道这一部分;)就像我写的,我可以改变视图元素的背景色,但是我如何在视图上绘制图形呢?也非常感谢你的回复。在我的问题中,我似乎没有明确表示,我实际上能够通过执行您和Axarydax的建议,很好地改变背景色。现在我真正的问题是如何在该视图上绘制图形?我会同意L0rdAli3n在上面的帖子中所说的,你的标题中不需要“Android编程”,这就是标签的作用。好的,谢谢,但我不允许使用标签“编程”,但当然我会在测试了提供的解决方案后,将答案标记为已接受。你觉得可以吗?还是你希望我在测试前接受答案?不,不,不。当然你先测试!但是,对于你的建议,除了我需要一个视图,而不是ImageView,而且你缺少一个结束括号之外,它是不起作用的。。。我将改进我的答案以澄清问题。耐心点。很抱歉缺少括号,我精简了我的示例并简化为:)正如您所说,您不想绘制,因此您不必关心是在视图还是在imageview上绘制,结果是相同的。当错误显示“必须定义显式构造函数”时,为什么不定义一个呢?在你搞乱CustomView和更高级的东西之前,请先了解一些关于Java和Android的基本知识。我编辑了我的答案,因此它包含了所需的构造函数,应该可以修复您的错误。首先,我非常感谢您的帮助,但请记住,您是说我必须使用CustomView类的人。一开始我不是那个“捣乱”的人。我只问如何利用main.XML创建的视图XML元素,但是仍然没有人回答我这个元素。
View v = (View)findViewById(R.id.Paper);
v.setBackgroundColor(myColor);
public class CustomView extends ImageView {
    // The needed constructors:
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setARGB(255, 150, 205, 129);
paint.setTextSize(20);
    canvas.drawText("Your Text", xposition, yposition, paint);
}
<com.example.CustomView
            android:id="@+id/yourID"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </com.example.CustomView>