我从xml创建了一个EditText,并在画布上绘制了很多东西。如何让EditText在画布上绘制的其他对象前面?
的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类
上设置它@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:
<?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>
当前代码不显示EditText对象。我不知道它是否仍在画布下或根本没有显示。有什么想法和解释吗?提前谢谢。
答案 0 :(得分:0)
只需在画布实例之后声明EditText
,因此避免在SurfaceHolder.Callback的SurfaceCreated
方法中初始化一个视图,因为这会导致在onDraw之前始终在背景中包含所有声明的内容它被称为;
只需尝试这个
@Override
public void surfaceCreated(SurfaceHolder holder){
// nice part to start all the background independiently to onDraw for example
Canvas canvas;
canvas.drawRGB(255,255,255);
// you will see that a white background will be drawed before all what you can put in onDraw class
}
@Override
public void draw(Canvas canvas){
//draw things
EditText et = (EditText)findViewById(R.id.editText1);
// here is where you can initializate the editText
}
答案 1 :(得分:0)
我没有看到您将EditText
添加到SurfaceView
的位置。因此,如果您只想添加EditText
而不添加任何父级(我的意思是RelativeLayout
在您的xml中),那么您需要创建仅包含EditText
的xml,例如调用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
:
@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().inflate(R.layout.edit_text, null);
编辑2:
要解决SurfaceView
没有addView
方法的错误,您可以改为创建FrameLayout
或RelativeLayout
,然后在其中添加SurfaceView
和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);