问题描述
这里已经阅读并测试了该主题的众多线程(大多数线程是Java线程,而不是Kotlin线程),我未能成功实现并且不了解我做错了什么
这是我的Portrait测试版面:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is my Portraittext"
tools:layout_editor_absoluteX="173dp"
tools:layout_editor_absoluteY="344dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我通过“创建景观..”创建了景观XML。这是景观文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is my Portraittext"
tools:layout_editor_absoluteX="173dp"
tools:layout_editor_absoluteY="344dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的Kotlin文件非常简单:
package com.example.mytest
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this,"in portrait",Toast.LENGTH_SHORT).show()
} else {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this,"in landscape",Toast.LENGTH_SHORT).show()
}
}
}
}
配置不会更改,也不会触发2个“ Toast”语句中的任何一个。 (在调试模式下,未输入onConfigurationChanged函数
我的AndroidManifest.xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyTest">
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="uiMode|orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这意味着:非常标准,仅扩展了
android:configChanges="uiMode|orientation|screenSize|keyboardHidden">
我检查了这行中的方向,screenSize,uiMode .....的几个条目,但是没有任何效果。当我直接强制使用横向模式时,显示将按预期方式转到横向。
我的代码有什么问题?
解决方法
我想结束我的问题。现在,它可以在我的智能手机上运行,而无需在模拟器上进行任何更改。可能很奇怪,抱歉