【发布时间】:2011-10-05 08:32:38
【问题描述】:
我正在尝试将自定义表面视图设为半透明。目前该活动是透明的,因此我可以在下面看到它之前的活动,但是一旦我添加了 20dp * 20 dp(称为 ControlsOverlayView)的自定义视图,它就会在屏幕上显示为黑色方块。我已经看到这篇帖子How to make a ListView transparent in Android? 并尝试设置视图的背景颜色、缓存提示和 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();
}
}
}
包含示例 TextView 和我的自定义 ControlsOverlayView 的 test.xml 布局是:
<?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>
再一次,它只是无法透明的自定义视图。有什么想法吗?
【问题讨论】:
标签: android view transparent