改变屏幕显示方向(同时切换横竖屏的图片)

来源:互联网 发布:网络诈骗超过2000 编辑:程序博客网 时间:2024/05/16 13:07

在学习改变横竖屏显示方向的例子的时候,我把例子 程序敲进去之后发现并没有如书上所示的在转换横竖屏的同时更换所显示的图片。由于一开始就设置了默认的竖屏图片,也就是说我在点击Button的时候屏幕的横竖状态发生了变化,但是图片木有改变,另外粗心的我当时也没有发现,按钮上的文字也没有随屏幕显示方向的改变而改变。

当时百度了一下也不懂得原因所在,后来自己静下心来重读main.java  发现了   由屏幕显示方向改变来触发的 onConfigurationChanged()  并没有被触发执行,那么原因在哪里呢?


1.想使用onConfigurationChanged()事件还需要在AndroidMainfest.xml  文件中进行一些配置(以下是修改了也不能不能触发onConfigurationChanged()的AndroidMainfest.xml 文档)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myclickdemo1"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|keyboard"
            android:screenOrientation="portrait"

            >
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission 
    android:name="android.permission.CHANGE_CONFIGURATION"
    />

</manifest>


2由于google在android3.2中添加了screensize改变的通知,在转屏的时候,不仅是orientation发生了改变,screensize同样也发生了改变.

(发现这个事件没有被触发后我就在相关方面找答案  很明显单单是屏幕的旋转还不足以触发这个事件,于是将AndroidMainfest.xml  改为如下便可在屏幕显示方向改变的同时切换图片了)


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myclickdemo1"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|keyboard|screenSize"
            android:screenOrientation="portrait"

            >
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission 
    android:name="android.permission.CHANGE_CONFIGURATION"
    />

</manifest>




以上    是我学习android以来 第一个自己独立思考去解决的问题,另外2012年的书会不会太老了,我感觉可能很多东西可能都有更新了

0 0
原创粉丝点击