Android的编程 - 如何存取权限[借鉴] XML视图main.xml中的布局,使用code [英] Android programming - How to acces [to draw on] XML view in main.xml layout, using code

查看:307
本文介绍了Android的编程 - 如何存取权限[借鉴] XML视图main.xml中的布局,使用code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我在的Andr​​oid 编程的新手,也很难与图形部分。了解 XML 创建布局文件之美,但却失去了如何访问各种要素,特别是View元素绘制就可以了。

Ok I'm a newbie at Android programming, have a hard time with the graphics part. Understand the beauty of creating layout in XML file, but lost how to access various elements, especially a View element to draw on it.

请参阅比如我的布局的main.xml 这里;

See example of my layout main.xml here;

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

正如你可以看到我有一个自定义的应用程序标题栏,那么查看填充中间,最后是两种按钮■在底部。醒目按钮 s'的事件和响应,例如改变标题栏文本和不断变化的查看背景颜色正常,但如何赫克我做访问和在的main.xml

As you can see I have a custom app title bar, then a View filling middle, and finally two buttons in the bottom. Catching buttons' events and responding to, for example changing title bar text and changing View background color works fine, but how the heck do I access and more importantly draw on the view defined in main.xml

更新的:
非常感谢您的建议,但除此之外,我需要一个查看,而不是的ImageView 和你缺少一个参数在 canvas.drawText()和结束支架,这是行不通的。

UPDATE: Thank you very much for your suggestion, however besides that I need a View, not ImageView and you are missing a parameter on canvas.drawText() and an ending bracket, it does not work.

现在,这是最有可能的,因为你错过了一个事实,即我是一个新手,假设我可以在任何填补空白。

Now this is most likely because you missed the fact that I am a newbie and assuming I can fill in any blanks.

现在首先我不明白,为什么在我的的main.xml 布局文件,我可以创建一个查看甚至 SurfaceView 元素,这正是我需要的,而是根据你的解决方案,我甚至不指定视图像&LT;查看.. ... /&GT;

Now first of all I do NOT understand why in my main.xml layout file I can create a View or even a SurfaceView element, which is what I need, but according to your solution I don't even specify the View like <View ....../>

反正我编辑我的的main.xml 根据您的解决方案,并精简下来一点为简单;

Anyways I edited my main.xml according to your solution, and slimmed it down a bit for simplicity;

<?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();

In my main java file MyApp.java. I added this after onCreate();

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

不过,我上的 CustomView 的部分错误;
隐超构造 ImageView的()是未定义默认constructor.Must定义一个明确的构造函数

But I get error on the CustomView part; "Implicit super constructor ImageView() is undefined for default constructor.Must define an explicit constructor"

Eclipse的建议3有关添加构造的快速修复,但没有帮助,以及它消除错误,但运行时应用程序提供了错误。

Eclipse suggests 3 quick fixes about adding constructor, but none helps, well it removes error but gives error on app when running.

我希望有人能打破这对我和提供解决方案,或许解释了为什么我不能创建我的的main.xml 布局文件视图元素并在其上绘制code。

I hope somebody can break this down for me and provide a solution, and perhaps explain why I can't just create a View element in my main.xml layout file and draw on it in code.

更新的:
好吧,我现在还在使用您的解决方案,这仍然是给我的问题和各种错误。但首先我还是不明白,为什么我要创建一个自定义类在我的布局使用,当我必须选择,甚至使用所见即所得布局编辑器插入视图元素和公正的,在我的主要的缺点,以及对现在只有code文件。所以pleeeeeease有人回答我这个问题之一。

UPDATE: Ok I am still trying to use your solution, which is still giving me problems and various errors. But first and foremost I still do NOT understand why I have to create a custom class to use in my layout, when I have to option to even use the wysiwyg layout editor to insert a View element and "just" draw on that in my main, well for now only code file. So pleeeeeease someone answer me that one.

反正现在我有;

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,在这里我直接粘贴您的code,所以它看起来像;

And I created a new class, new file CustomView.Java, where I directly pasted your code so it looks like;

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

好了,所以这个类code要我加入导入 S代表的ImageView 画布油漆,我照做了,然后我再次回到了错误的公共类 * CustomView * 部分;
隐超构造ImageView的()是未定义默认constructor.Must定义一个明确的构造函数。<​​/ em>的

Ok so this class code wants me to add imports for ImageView, Canvas and Paint, which I did, and then again I am back to the error the public class *CustomView* part; "Implicit super constructor ImageView() is undefined for default constructor.Must define an explicit constructor".

我AP $ P $你想帮我pciate,谢谢!

I appreciate you trying to help me, THANKS!

推荐答案

要借鉴视图,创造自己的自定义视图。

To draw on a view, creat your own custom view.

要做到这一点,你需要添加一个新的类。与Eclipse它只是文件 - >新建 - >类和你去。重写的onDraw() - 方法,以满足您的需求,并添加您CustomView到你的布局:

To do so, you need to add a new class. With eclipse it's just File->New->Class and there you go. Override the onDraw()-Method to fit your needs and add your CustomView to your Layout:

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是这样的:

In your xml-File you add your CustomView like this:

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

如果您wan't吸引到您的RES-文件夹被拉伸,specifiy源 - 参数的customView的。

If you wan't to draw onto a drawable from your res-Folder, specifiy the "src"-parameter of your customView.

这篇关于Android的编程 - 如何存取权限[借鉴] XML视图main.xml中的布局,使用code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆