如何使XML对象在画布上绘制对象面前露面 [英] How to make xml object show up in front of drawn objects in canvas

查看:290
本文介绍了如何使XML对象在画布上绘制对象面前露面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的XML和在画布上绘制的许多事情一个EditText。如何使EditText上是在画布上绘制的其他对象的前面?

I created an EditText from xml and many things drawn on canvas. How to make EditText to be in front of the other objects drawn on canvas?

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/mainLayout"
android:clickable="true">

</RelativeLayout>

和我设置的自定义SurfaceView类

and I set this on a custom SurfaceView class

@Override
public void surfaceCreated(SurfaceHolder holder){

    thread = new MainThread(getHolder(), this); //Canvas is instatiated inside thread class
}

@Override
public void draw(Canvas canvas){
    //draw things
    EditText et = (EditText)findViewById(R.id.editText1);
}

主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //Remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Set fullscreen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(new GamePanel(this)); //GamePanel is the custom SurfaceView class

}

edit_text.xml:

edit_text.xml :

<?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText1"
    android:text="Hello World">
</EditText>

目前的code没有显示出的EditText对象。我不知道是否仍是帆布下或没有显示在所有。任何想法和解释?先谢谢了。

The current code shows no EditText object. I do not know whether it is still under the canvas or not shown at all. Any idea and explanation? Thanks in advance.

推荐答案

我没看到你添加的EditText 你的 SurfaceView 。所以如果你想仅仅只添加的EditText 无父(我的意思是 RelativeLayout的在你的XML),那么你需要创建一个包含XML 的EditText ,堪称例如 edit_text.xml

I didn't see where you add your EditText to your SurfaceView. So if you want just add only EditText without any parent (I mean RelativeLayout in your xml), then you need to create xml containing only EditText, called for example edit_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText1"
    android:text="Hello World" />

和那么你应该夸大它,并添加到您的的GamePanel

And then you should inflate it and add to your GamePanel:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //Remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Set fullscreen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    GamePanel gamePanel = new GamePanel(this);
    EditText editText = (EditText) getLayoutInflater().inflate(R.layout.edit_text, null);
    gamePanel.addView(editText);

    setContentView(gamePanel); //GamePanel is the custom SurfaceView class

}

编辑:

我忘了投膨胀以的EditText ,所以它应该看起来像的EditText EDITTEXT =(EditText上)getLayoutInflater()。膨胀(R。 layout.edit_text,NULL);

I forgot to cast inflated view to EditText, so it should look like EditText editText = (EditText) getLayoutInflater().inflate(R.layout.edit_text, null);

编辑2:

要解决 SurfaceView 没有你可以创建一个 addView 方法错误>的FrameLayout 或 RelativeLayout的代替,并添加有你的 SurfaceView 的EditText

To resolve the error that SurfaceView doesn't have addView method you can create a FrameLayout or RelativeLayout instead, and add there your SurfaceView and EditText:

    GamePanel gamePanel = new GamePanel(this);
    RelativeLayout.LayoutParams gameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    gamePanel.setLayoutParams(gameParams);

    RelativeLayout content = new RelativeLayout(this);
    EditText editText = (EditText) getLayoutInflater().inflate(R.layout.edit_text, null);

    content.addView(gamePanel);
    content.addView(editText);

    setContentView(content);

这篇关于如何使XML对象在画布上绘制对象面前露面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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