Android Studio-如何创建显示在当前视图上方的透明视图
所以我有一个活动,它有自己的XML文件,我有一个单独的视图,它只使用画布,没有XML。代码在这里解释,问题和问题在代码下面 这是第一个使用canvas do draw的视图:Android Studio-如何创建显示在当前视图上方的透明视图,android,xml,android-canvas,transparency,android-view,Android,Xml,Android Canvas,Transparency,Android View,所以我有一个活动,它有自己的XML文件,我有一个单独的视图,它只使用画布,没有XML。代码在这里解释,问题和问题在代码下面 这是第一个使用canvas do draw的视图: public class BallView extends View { public BallView (Context context) { super(context); WindowManager wm = (WindowManager) context.getSystemS
public class BallView extends View {
public BallView (Context context) {
super(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
...//create Ball objects
}
}
实际在画布上绘制的球对象:
public Ball(int xMax, int yMax) {
bounds = new RectF();
paint = new Paint();
...(initialize variables)
}
public void begin(Canvas canvas){
bounds.set(...);
paint.setColor(...);
canvas.drawOval(bounds, paint);
}
以下是调用上述视图的活动:
public class CircleBounceScreen extends ActionBarActivity{
View ballView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ballView = new BallView(this);
setContentView(ballView);
.... //do stuff
}
// some triggermethod
public void trigger(){
setContentView(R.id.activity_main);
//fade the button in
//wait for the button to be clicked
}
}
现在,在activity_主XML文件中有一个按钮
<LinearLayout
...alignment stuff >
<Button android:id="@+id/restart_button"
...design stuff
android:visibility="gone"/>
</LinearLayout>
ballView执行一段时间后,它通过侦听器让活动知道。这就是我想要发生的事情;ballView保持在视图中,就在背景中,XML文件中的按钮显示在其上方。这将允许点击ballView(基本上冻结)和按钮
我已经查看了所有的堆栈溢出,所有的解决方案都使用了透明的活动背景(但是更改setContentView(activity_main)会删除以前的视图,透明与否无关紧要),或者,它们只是在同一个XML文件中定义了所有内容,这样它们就可以简单地将ContentView设置为该XML文件,并且效果良好。请注意,我不能这样做,因为我的一个视图使用画布,另一个使用XML
那么,如何同时显示两个视图,其中一个视图透明地位于另一个视图的下方呢?为什么不在XML中包含ball视图
<RelativeLayout>
<Button android:id="@+id/restart_button"
...design stuff
android:visibility="gone"/>
<com.packagename.BallView
android:id="@id/ball_view"/>
</RelativeLayout>
您可能需要向BallView添加额外的构造函数,以允许它在XML中膨胀:
public class BallView extends View {
public BallView (Context context) {
this(context, null);
}
BallView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
BallView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
...//create Ball objects
}
}
这在Android开发者网站上得到了最好的解释。但是AttributeSet允许您在XML中定义某些内容,例如球的颜色,您可以稍后在代码中进行设置。这些构造函数的设置方式是,无论调用3个构造函数中的哪一个,都会调用带有设置的构造函数。第一个呼叫第二个,第二个呼叫第三个。请注意每个上的参数数量。应该显示它,因为您以与上面类似的方式包含它。将允许您向其添加布局参数,如android:layout\u height=“match\u parent”,BallView中的所有内容都可以保持不变,但您至少需要添加构造函数。您只需要将BallView包含在XML中并具有正确的包名,它就会显示出来。
public class BallView extends View {
public BallView (Context context) {
this(context, null);
}
BallView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
BallView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
...//create Ball objects
}
}