2011-07-14 47 views
1

可能重複:
how to make surfaceview transparent製作定製的Android SurfaceView透明

我試圖做一個自定義的表面觀半透明狀。目前該活動是透明的,所以我可以在它下面看到活動,但只要添加20dp * 20dp(稱爲ControlsOverlayView)的自定義視圖,它就會在屏幕上顯示爲黑色方塊。我看過這個帖子How to make a ListView transparent in Android?,並試圖設置背景顏色,cachehint和alpha的視圖無濟於事。

透明活性容納視圖是ControlsOverlayActivity.java:

public class ControlsOverlayActivity extends Activity { 

    private ControlsOverlayView overlay; 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 
     TextView textView = (TextView) findViewById(R.id.test_text); 
     try { 
      textView.setText("test"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

的佈局的test.xml包含樣本的TextView和我的自定義ControlsOverlayView是:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/test" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView android:id="@+id/test_text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

    <wp.ui.ControlsOverlayView 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:background="#00000000" 
    android:cacheColorHint="#00000000" 
    android:alpha="0" 
    /> 

</FrameLayout> 

和視圖:

public class ControlsOverlayView extends SurfaceView implements Runnable, Callback{ 

    public ControlsOverlayView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     // make sure we get key events 
     setFocusable(true); 
     setFocusableInTouchMode(true); 
     requestFocus(); 

     // register our interest in hearing about changes to our surface 
     getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
     //resize(getWidth(), getHeight()); 
     //paintControls(); 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
    } 

} 

也在我的清單中我有:

<activity android:name=".ui.ControlsOverlayActivity" android:theme="@style/Theme.Transparent"> 
     </activity> 

和Theme.Transparent定義爲:

<style name="Theme.Transparent" parent="android:Theme"> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowBackground">@color/transparent</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:windowNoTitle">true</item> 
     <item name="android:backgroundDimEnabled">false</item> 
    </style> 

再次它只是自訂認爲`噸得到是透明的。有任何想法嗎?

回答