how to add & remove layout element between portarit mode and landscape mode?
I have a simple layout, which shows a text and a image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TEST"
android:textColor="#0099cc"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#55ff0000"
android:src="@drawable/my_image"/>
</LinearLayout>
</LinearLayout>
I would like the image to be shown only when the device is in Portrait mode.
What I mean is if the device is in landscape mode, I would like to not only just hide the image, but remove 开发者_开发知识库the image completely from the layout. How to do it?
Android provides for this by allowing you to have multiple layout files in separate layout folders which are used in different environments.
You should move this xml file to a new res/layout-port
folder in your directory structure, and create a copy in res/layout-land
which lacks your ImageView
. Android will select the appropriate XML file depending on which orientation the device is in.
There's more information here.
You can specify two layouts: one fort portrait mode, placed in layout-port folder, and one for landscape mode, placed in layout-land folder. Hope this helps.
精彩评论