2011-12-09 79 views
2

为什么下面的代码失败?添加按钮后,为什么我的背景会消失?

我在我的main.xml中有一个蓝色的relativeLayout。点击我添加一个绿色按钮。

在我的摩托罗拉Xoom上运行时,单击屏幕,我看到显示绿色按钮,但背景从蓝色变为黑色。如果我再次点击我的蓝色背景显示。再次点击,我看到黑色...

我错过了什么?

感谢您的任何帮助。

package com.android.mikeviewtester; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class ViewTesterActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // set the relative layout as our view 
     setContentView(R.layout.main); 

     RelativeLayout currentView = (RelativeLayout) findViewById(R.id.MyRelativeLayout); 

     // set a listener 
     currentView.setOnTouchListener((android.view.View.OnTouchListener) mOnTouchListener); 
    } 

    private android.view.View.OnTouchListener mOnTouchListener = new android.view.View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 

      if (v != null) 
       v.onTouchEvent(event); 

      if (event.getAction() == MotionEvent.ACTION_UP) { 

       android.widget.RelativeLayout vw = (android.widget.RelativeLayout) findViewById(R.id.MyRelativeLayout); 

       // create and add a new cyan button 
       Button btn = new Button(ViewTesterActivity.this); 
       btn.setBackgroundColor(Color.GREEN); 
       vw.addView(btn, 100, 100); 
       vw.invalidate(); 
       btn.invalidate(); 
      } 

      return true; 
     } 
    }; 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/MyRelativeLayout" 
    android:background="#0000FF"> 
</RelativeLayout> 
+1

可能有一些跟你的新的按钮设置viewgroup.layoutparams,我不知道,将不得不摆弄它;)添加aswell后尝试无效? – Warpzit

+0

我没有运气就玩过layoutparams设置。尝试使用addView(btn,100,100)来避免layoutparams。我试过invalidate()和invalidateRect(..)。当我在视图上调用getDrawingRect()时,我得到整个画布矩形,使得看起来没问题。 – SkolVikingsGuy

回答

2

我认为这个问题来自于在onCreate()的en上调用setContentView()方法。 尝试这样

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // set the relative layout as our view 
    setContentView(R.layout.main); 
    currentView = (RelativeLayout)findViewById(R.id.main); 

    // create a red button 
    Button btn = new Button(this); 
    btn.setBackgroundColor(Color.RED); 
    Relative.LayoutParams params = new Relative.LayoutParams(100, 100); 
    currentView.addView(btn, params); 

    // setup the view (blue background) 
    currentView.setBackgroundColor(Color.BLUE); 
    currentView.setOnTouchListener((android.view.View.OnTouchListener) mOnTouchListener); 
} 

和你的main.xml文件应该是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/main" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
+0

感谢您的建议,我希望能做到这一点。但我仍然看到这个问题。我更新了上面的代码以显示我目前的内容。 – SkolVikingsGuy

+0

我只是简化了代码,在xml中设置了bg颜色,并且我没有在onCreate()中添加按钮。开始怀疑是否将它添加到监听器中是问题所在。不知道为什么。 – SkolVikingsGuy

+1

好吧,试试vw.postInvalidate();代替vw.invalidate();并在你的onTouch()方法中删除btn.invalidate()。 – jobesu14

1

我不知道是否有错误是从即将到来,但你没有使用正确的布局PARAMS。 您应该使用视图的布局参数是来自父级的参数。 在你的情况,你应该使用RelativeLayout.LayoutParams作为你的按钮,我想为你的相对布局设计一个FrameLAyout.LayoutParams。

+0

谢谢,即使它没有引起这个问题,也很高兴知道。我更新了上面的代码(仍然打破)。 – SkolVikingsGuy

相关问题