简体   繁体   中英

On changing screen orientation to LANDSCAPE initially, triggers sensor orientations then loads in PORTRAIT

I am creating an app where I need to set LANDSCAPE screen rotation manually, and then it should work as per the ORIENTATION sensor. But on setting screen rotation to LANDSCAPE initially, it triggers multiple orientation changes and then loads layout in PORTRAIT mode. This is what I have tried till now.

src/main/java/com/example/myorientation/MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btn = findViewById<Button>(R.id.btn)

        requestedOrientation = if(savedInstanceState == null) {
            ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
        }else ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

    }
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // Checks the orientation of the screen
        if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show()
        } else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show()
        }
    }
}

src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myorientation"
android:versionCode="1"
android:versionName="1.0">
<application
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/Theme.MyOrientation"
    android:label="@string/app_name">
    <activity android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
</manifest>

src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="click" />

</RelativeLayout>

Have you created a separate file for the landscape mode?

Create a new folder under your /res directory and call it layout-land in Android Studio and put your layout xml files for the landscape with the same naming.

Totally agree with this as Android Studio gives you an option to create a separate one upon request. Ensure any layout_width and layout_heights are defined as match_parent doesn't seem to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM