十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在项目开发中,
关于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"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/button1_landscape"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LandScape"/>
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="横屏界面"
android:textAppearance="?android:attr/textAppearanceLarge"/>
//portrait_screen.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScreenActivity">
android:id="@+id/button1_portrait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Portrait"/>
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1_portrait"
android:gravity="center"
android:text="竖屏界面"
android:textAppearance="?android:attr/textAppearanceLarge"/>
//manifest.xml
"1.0"encoding="utf-8"?>
package="com.example.testscreen"
android:versionCode="1"
android:versionName="1.0">
android:minSdkVersion="8"
android:targetSdkVersion="18"/>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
android:name="com.example.testscreen.ScreenActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。