2011-12-09 82 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

相關問題