2012-01-04 45 views
2

我看到了關於Honeycomb的繪圖問題,我找不出來。出於某種原因,當我在代碼中將按鈕添加到視圖時,視圖的背景消失(變爲黑色)。蜂窩背景繪圖問題

這工作正常我的銀河選項卡運行2.3。它在模擬器或運行3.2的我的Motorolla Xoom中失敗。

詳細說明:

在的onCreate()我設置任一背景顏色或main.xml中限定在RelativeLayout的一個背景圖像。我的relativeLayout被定義爲fill_parent。

我有一個OnTouchListener用於我的活動,其中我向兩個綠色按鈕(一個左對齊,一個右對齊)添加到relativeLayout。

當我添加這兩個按鈕時,relativeLayout的背景消失(全黑顯示)。我無法解釋爲什麼。

線索:

  • 如果我設置了一個綠按鈕的顏色改爲Color.TRANSPARENT,一切工作和我的背景不會消失。這似乎是一個很大的線索,但我無法弄清楚它的含義。當我使用彩色背景時,如果我將targetSdkVersion設置爲「11」而不是「7」(我的目標是2.1/7),它可以工作,並且我的顏色背景不會消失。但圖像仍然被打破。

  • 當使用彩色背景時,我可以調用setDrawingCacheBackgroundColor(Color.RED),這會導致紅色背景而不是黑色。我可以將此作爲解決方案,將緩存顏色設置爲我的背景顏色,但在使用圖像時不起作用。

這真的感覺像是一個在android中的錯誤,因爲我看不到我在代碼中做錯了什麼。我會很感激任何幫助或建議。

這裏是我的簡單的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/MyLayout"> 
</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); 

     // get the relative layout 
     android.widget.RelativeLayout layout = (android.widget.RelativeLayout) findViewById(R.id.MyLayout); 

     // set an on touch listener 
     layout.setOnTouchListener((android.view.View.OnTouchListener) mOnTouchListener); 

     // set a background image 
     // PROBLEM: this background image disappears when adding the buttons in our touch handler 
     // GOOFY FIX: If I change the color of one of the buttons we add to be Color.TRANSPARENT, this 
     //  background image doesn't disappear. 
     layout.setBackgroundResource(R.drawable.icon); 
     layout.setDrawingCacheEnabled(false); 

     // alternatively, set background color 
     // PROBLEM: the color disappears (becomes black) when adding the button in our touch handler 
     // GOOFY FIX: If I change the color of one of the buttons we add to be Color.TRANSPARENT, the color 
     //    doesn't disappear 
     // GOOFY FIX 2: If I set android:targetSdkVersion to "11" in our manifest, the color doesn't disappear 

     // CLUE?: Without any fixes above and using color as the background, if I set the drawing cache color 
     // to Color.RED it'll show up instead of black. This would be a great solution to my problem but it 
     // doesn't work for background images. 
     // layout.setDrawingCacheBackgroundColor(Color.RED); 
     // layout.setDrawingCacheEnabled(true); 
    } 

    // on touch listener, add two buttons to the view 
    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 layout = (android.widget.RelativeLayout) findViewById(R.id.MyLayout); 

       // add a green button, left aligned 
       Button btn = new Button(ViewTesterActivity.this); 
       btn.setBackgroundColor(Color.GREEN); 

       // GOOFY FIX: setting this instead of green solves the issue, no disappearing background 
       // btn.setBackgroundColor(Color.TRANSPARENT); 

       RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100); 
       params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
       layout.addView(btn, params); 

       // add a green button, right aligned 
       btn = new Button(ViewTesterActivity.this); 
       btn.setBackgroundColor(Color.GREEN); 
       params = new RelativeLayout.LayoutParams(100, 100); 
       params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
       layout.addView(btn, params); 
      } 
      return true; 
     } 
    }; 
} 
+0

我在上不正確測量的XOOM相對佈局類似的問題:

protected static final int REFRESH = 0; private Handler _hRedraw; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... _hRedraw=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case REFRESH: redrawEverything(); break; } } }; } private void redrawEverything() { android.widget.RelativeLayout layout = (android.widget.RelativeLayout) findViewById(R.id.MyLayout); layout.invalidate(); layout.refreshDrawableState(); } 
OnTouchListener

現在只需使用發送郵件到您的處理。目前還沒有解決方案。 – Theblacknight 2012-02-09 14:49:47

回答

0

我遇到了莫名其妙類似的問題,我得到它通過處理工作。您需要將消息發送到在您的活動中實施的處理程序,並使視圖無效。不需要setDrawingCacheEnabled(false)。這裏是我做了我的問題:

_hRedraw.sendEmptyMessage(REFRESH);