Android 编程 - 如何使用代码访问 main.xml 布局中的 [to draw on] XML View

标签 android xml layout view drawing

好的,我是 Android 的新手编程,图形部分很难。在 XML 中了解创建布局的美妙之处文件,但丢失了如何访问各种元素,尤其是要在其上绘制的 View 元素。

查看我的布局示例 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>

如您所见,我有一个自定义应用 title bar , 然后是 View填充中间,最后两个button在底部。抓捕 button s 的事件和响应,例如更改标题栏文本和更改 View背景颜色工作正常,但我到底如何访问,更重要的是如何绘制 main.xml 中定义的 View ?

更新: 非常感谢您的建议,但除此之外我还需要一个 View , 不是 ImageView并且您在 canvas.drawText() 上缺少一个参数和结束括号,它不起作用。

现在这很可能是因为您忽略了我是新手这一事实,并假设我可以填补任何空白。

现在首先我不明白为什么在我的main.xml布局文件我可以创建一个 View甚至是 SurfaceView元素,这是我需要的,但根据您的解决方案,我什至没有指定像 <View ....../> 这样的 View

无论如何,我编辑了我的 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">

           <com.example.MyApp.CustomView 
                android:id="@+id/Paper" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" />
           <com.example.colorbook.CustomView/>

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

在我的主 java 文件中 MyApp.java .我在 onCreate() 之后添加了这个;

public class CustomView extends ImageView {
     @Override
     protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          canvas.drawText("Your Text", 1, 1, null);
     }
}

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

Eclipse 建议了 3 个关于添加构造函数的快速修复,但都没有帮助,它消除了错误,但在运行时在应用程序上给出了错误。

我希望有人能为我解决这个问题并提供解决方案,或许还能解释为什么我不能在我的 main.xml 中创建一个 View 元素。布局文件并在代码中绘制。

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

反正现在我有;

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);
     }
}

好的,所以这个类代码要我添加 import s ImageView , CanvasPaint ,我做到了,然后我又回到了公共(public)课的错误*CustomView*部分; “默认构造函数未定义隐式 super 构造函数 ImageView()。必须定义一个显式构造函数”。

我感谢你试图帮助我,谢谢!

最佳答案

要在 View 上绘制,请创建您自己的自定义 View 。

为此,您需要添加一个新类。对于 Eclipse,它只是 File->New->Class,然后就可以了。覆盖 onDraw()-Method 以满足您的需要并将 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);
    }<p></p>

<pre><code>@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);
}
</code></pre>

<p>}</p>

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

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

如果您不想从您的 res 文件夹中绘制一个可绘制对象,请指定您的 customView 的“src”参数。

关于Android 编程 - 如何使用代码访问 main.xml 布局中的 [to draw on] XML View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4548194/

相关文章:

css - Div 在添加元素时移动一个 div

android - 垂直对齐重力没有响应

android - 如何在 Android 的 loopj 中支持 Comodo SSL 证书?

java - 绑定(bind)声明命名空间 "http://annox.dev.java.net"将被忽略,因为它不是由 jaxb :extensionBindingPrefixes attribute 指定的

Android:管理传递给 CursorAdapter 的光标

xml - 使用 XQUERY 从 XML 中消除单个元素类型

iPhone XML SOAP 请求或 .NET Web 服务

java - 如何使用 GridBagLayout 将组件放在右下角?

android - 无法从 Activity A 获取 Intent 到 Activity B

android - NestedScrollView 内的 MapView 不滚动