在项目开发中,
创新互联专注于高平企业网站建设,响应式网站建设,
商城网站定制开发。高平网站建设公司,为高平等地区提供建站服务。全流程按需开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
关于android手机横竖屏切换时显示不同的界面,在这里我定义了两个xml布局文件
landscape_screen.xml,portrait_screen.xml
根据屏幕的旋转切换不同的布局文件
重写onConfigurationChanged方法,对其进行监听并判断当前的屏幕状态,根据其状态显示对应的布局文件
当然在manifest.xml中对应的activity中要加上
android:configChanges="keyboardHidden|orientation|screenSize">
贴上代码说话
public classScreenActivity extends Activity implements OnClickListener{
private Button btn;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//默认为竖屏
setupViewInPortraitLayout();
}
//竖屏界面
private voidsetupViewInPortraitLayout(){
setContentView(R.layout.portrait_screen);
btn = (Button)findViewById(R.id.button1_portrait);
btn.setOnClickListener(this);
}
//横屏界面
private voidsetupViewInLandscapeLayout(){
setContentView(R.layout.landscape_screen);
btn = (Button)findViewById(R.id.button1_landscape);
btn.setOnClickListener(this);
}
@Override
public voidonConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(ScreenActivity.this,"onConfigurationChanged", Toast.LENGTH_LONG).show();
Configuration cfg =getResources().getConfiguration();
if (cfg.orientation ==Configuration.ORIENTATION_LANDSCAPE) {
setupViewInLandscapeLayout();
} else if(cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {
setupViewInPortraitLayout();
}
}
@Override
public void onClick(Viewarg0) {
// TODO Auto-generatedmethod stub
if(arg0.equals(btn)){
Toast.makeText(ScreenActivity.this,"Click", Toast.LENGTH_LONG).show();
}
}
}
//landscape_screen.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">