效果图
方案一竖屏.png方案一横屏.png
方案二竖屏.png
方案二横屏.png
方案一:不重走生命周期
Activity代码
public class ScreenChangeActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_change_portrait);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {//当切换为横屏时,显示横屏的布局
setContentView(R.layout.activity_screen_change_landscape);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {//当切换为竖屏时,显示竖屏的布局(默认竖屏的布局)
setContentView(R.layout.activity_screen_change_portrait);
}
}
}
竖屏布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBC05"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#EA4335"
android:gravity="center"
android:text="我是竖屏的布局"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="我不重走生命周期"
android:textColor="#FFFFFF"
android:textSize="13sp"
android:textStyle="bold" />
</LinearLayout>
横屏布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4285F4"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#34A853"
android:gravity="center"
android:text="我是横屏的布局"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="我不重走生命周期"
android:textColor="#FFFFFF"
android:textSize="13sp"
android:textStyle="bold" />
</LinearLayout>
AndroidManifest.xml配置代码
<activity
android:name=".screen.ScreenChangeActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
方案二:重走生命周期
Activity代码
public class ScreenChange2Activity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_change2);
}
}
layout布局
image.png
竖屏布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBC05"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#EA4335"
android:gravity="center"
android:text="我是竖屏的布局"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="我重走生命周期"
android:textColor="#FFFFFF"
android:textSize="13sp"
android:textStyle="bold" />
</LinearLayout>
横屏布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4285F4"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#34A853"
android:gravity="center"
android:text="我是横屏的布局"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="我重走生命周期"
android:textColor="#FFFFFF"
android:textSize="13sp"
android:textStyle="bold" />
</LinearLayout>
AndroidManifest.xml配置代码
<activity
android:name=".screen.ScreenChange2Activity"/>
网友评论