全面解析Android应用开发中Activity类的用法

2019-12-10 18:55:24于海丽

六、一些关于Activity的技巧
1.设置Activity的方向

android:screenOrientation="portrait">// 竖屏 , 值为 landscape 时为横屏

2.设置Activity全屏
在onCreate方法中添加如下代码:

// 设置全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
 WindowManager.LayoutParams.FLAG_FULLSCREEN); 
// 去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);

改变窗口大小、位置、透明度
在onCreate方法中添加如下代码:

Window w=getWindow();
w.setBackgroundDrawableResource(resourceID);//设置窗口背景
WindowManager.LayoutParams layoutParams = w.getAttributes();
layoutParams.height = 200; 
layoutParams.width= 200;
layoutParams.gravity = Gravity.TOP;
layoutParams.x=50;//距离Gravity属性的距离
layoutParams.y=50;
layoutParams.alpha = 0.5;//0:完全透明,1:不透明
w.setAttributes(layoutParams);

3.关闭所有窗口

Intent intent = new Intent(); 
intent.setClass(Android123.this, CWJ.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置 
startActivity(intent)

另一种方法:在调用退出方法中写上MyApplication.getInstance().exit();