在RadioGroup中切换Fragment,其中一个Fragment支持重力感应设置横竖屏,其他的都是竖屏。

首先设置
 

<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,//根据重力变换朝向

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要实现不在同一个 RadioGroup 的两个 RadioButton 只能选一个的话,可以通过以下两种方式来实现: 1. 使用 RadioGroup 和 LinearLayout 组合实现: 在布局文件,将两个 RadioButton 放在同一个 LinearLayout ,然后再将 LinearLayout 放在 RadioGroup ,这样就可以实现两个 RadioButton 只能选一个的效果。具体代码如下: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" /> </LinearLayout> </RadioGroup> ``` 2. 使用自定义属性实现: 在布局文件,给两个 RadioButton 设置一个自定义属性,然后在代码监听两个 RadioButton 的点击事件,当一个 RadioButton 被选时,将另一个 RadioButton 的选状态设置为未选。具体代码如下: 在 res/values/attrs.xml 文件定义自定义属性: ```xml <declare-styleable name="CustomRadioButton"> <attr name="exclusive" format="boolean" /> </declare-styleable> ``` 在布局文件,给两个 RadioButton 设置相同的自定义属性: ```xml <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" app:exclusive="true" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" app:exclusive="true" /> ``` 在代码,监听两个 RadioButton 的点击事件,并在事件将另一个 RadioButton 的选状态设置为未选: ```java RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); radioButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton2.setChecked(false); } }); radioButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton1.setChecked(false); } }); ``` 以上是两种实现不在同一个 RadioGroup 的两个 RadioButton 只能选一个的方法,您可以根据自己的需求选择其一种。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值