您的位置:首页 > 移动开发 > Android开发

横竖屏切换和contentView的三种显示方式

2016-03-22 08:49 531 查看
较常用,记下来方便查询

android:configChanges="orientation|keyboardHidden|screenSize"//加了之后横竖屏切换不重新创建Activity。

android:screenOrientation="landscape"//只横屏

android:screenOrientation="portrait"//只竖屏

//点击转换屏幕方向。

public void switchOrientation(View v) {
//1. 得到当前方向
int orientation = getResources().getConfiguration().orientation;
//2. 设置新的方向
if(orientation==Configuration.ORIENTATION_PORTRAIT) {
//变为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}

显示contentView的三种方式

1.setContentView(R.layout.activity_main)

2.View view = View.inflate(this, R.layout.activity_main, null);

setContentView(view);

3.MyTextView tv = new MyTextView(this);

setContentView(tv);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发