Android screen orientation use different xmls (switching/rotating screen landscape and portrait)

StackOverflow https://stackoverflow.com/questions/21177922

문제

I want to develop an android application for perfectly screen design arranged for portrait and landscape modes. and should have to give the screen orientation facility to user. how to change two xml files of the application when change the screen orientation of the device?

도움이 되었습니까?

해결책 2

create another layout folder called layout-Land under the res folder. copy all landscape xml files to that layout -Land folder with the same name. Recheck all portrait xml files are in the layout folder. add android:configChanges="orientation" to the manifest

 <activity
        android:name=".LoginActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateVisible|adjustPan" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

cheerz

다른 팁

do not add android:configChanges="orientation" it'll stop recreation of activity which ultimately not allows you to have vertical and horizontal layout applicable.

Just make another layout and put it in your layout-land folder. Your structure should look like this:

res/
    layout/ 
        my_layout.xml #will be used in portrait
    layout-land/ 
        my_layout.xml #will be used in landscape

Do not add the android:configChanges attribute as suggested -- as @nitin said, that will prevent your Activity from being recreated and you'll have to do some extra work to handle resource switching. When your Activity is created, and you create the layout (e.g. setContentView(R.layout.my_layout.xml), it will resolve the right layout resource based on the orientation.

Add this code in manifest file in activity tag:

For landscape mode:

<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

For portrait mode:

<activity android:name="MyActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top