我正在开发页面上开发一个带有TextView和ImageView的简单Android应用程序。当我将手机旋转到横向模式时,应用程序崩溃了。虽然我看到该应用程序没有被杀死,但它只是消失了,我可以看到它仍然在Android Studio中运行。 以下是我的代码:
Content_main.xml -landscape模式
<?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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.viral.myapplication2.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ganpati_bappa_moraya"
android:id="@+id/textView"
tools:context=".Blink"
android:textAlignment="center"
android:textSize="@dimen/abc_action_button_min_width_overflow_material"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_centerHorizontal="true"
android:src="@mipmap/god_pic"
android:layout_below="@+id/textView" />
</RelativeLayout>
Content_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.viral.myapplication2.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
<TextView
android:layout_width="400dp"
android:layout_height="100dp"
android:text="Ganpati Bappa Moraya !!!"
android:id="@+id/textView"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
tools:context=".Blink"
android:layout_marginTop="84dp"
android:textAlignment="center"
android:textSize="@dimen/abc_action_button_min_width_overflow_material" />
<ImageView
android:layout_width="500dp"
android:layout_height="200dp"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="@mipmap/god_pic" />
</RelativeLayout>
Android_Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.viral.myapplication2">
<application
android:allowBackup="true"
android:icon="@mipmap/god_pic"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:2)
将AndroidManifest.xml文件修改为此
android:configChanges="keyboardHidden|orientation|screenSize".
screenSize
属性将关注屏幕方向更改,活动生命周期不会再次调用。