首先设置
<activity android:name=".MainActivity"
android:screenOrientation="portrait"//Activity进入时为竖屏
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/fl_top"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/rg"
android:layout_height="0dp"/>
<RadioGroup
android:layout_width="match_parent"
android:id="@+id/rg"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="48dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="text1"
android:id="@+id/rb1"
android:layout_height="match_parent" />
<RadioButton
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="text2"
android:id="@+id/rb2"
android:layout_height="match_parent" />
<RadioButton
android:layout_width="wrap_content"
android:layout_weight="1"
android:id="@+id/rb3"
android:text="text3"
android:layout_height="match_parent" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
Activity的代码 这里是BlankFragment1支持重力感应,其他两个只支持竖屏
public class MainActivity extends AppCompatActivity {
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup=findViewById(R.id.rg);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch (checkedId){
case R.id.rb1:
fragment=new BlankFragment1();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);//重力感应
break;
case R.id.rb2:
fragment=new BlankFragment2();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
break;
case R.id.rb3:
fragment=new BlankFragment3();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
fragmentTransaction.replace(R.id.fl_top,fragment);
fragmentTransaction.commit();
}
});
findViewById(R.id.rb1).performClick();
}
String TAG=this.getClass().getName();
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Activity的要显示或者隐藏的的控件在这里控制,Fragment也在相应的类的onConfigurationChanged方法设置一些想做的事。
Log.i(TAG, "onConfigurationChanged: ");
if (fragment instanceof BlankFragment1){//当监听到切换横竖屏时,要重新加载页面,不然Fragment的页面是不改变的
fragment=new BlankFragment1();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_top,fragment);
fragmentTransaction.commit();
}
}
}
同时在res中建立一个layout-land文件夹用于存放横屏的页面,也可以在
onCreateView中判断横竖屏去加载相应的布局。
//ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR 无论手机是否设置了自动转屏都会转动(目前测试结果暂时如此,不知道是不是全部手机都这样)
//
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED 这个是跟随手机设置去转动。如:手机没有开启自动转屏就无法横屏了。
抄录一下别人的信息:
Activity屏幕属性有以下几种:
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,//指定横屏
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,//指定竖屏
ActivityInfo.SCREEN_ORIENTATION_USER,//根据用户朝向
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,//不受重力影响
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,//横屏动态转换
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,//竖屏动态转换
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR,//根据重力变换朝向