Android 程式开发:(七)适应屏幕的方向 —— 7.3重新调整尺寸和位置
2012-04-24 16:12
399 查看
如果想根据屏幕的方向自定义UI,除了把views锚定在屏幕的四周(上一节讲过"锚定"视图),更简单的办法就是创建一个独立的res/layout文件夹,它包含了不同屏幕方向下的UI布局。如果想要支持landscape横屏模式,那么就可以在res文件夹下面创建一个layout-land文件夹(land代表landscape)。
示例图:
基本上,在layout文件夹下面的main.xml定义了在portrait竖屏模式下activity的布局。但在layyout-land文件夹下面的main.xml定义了横屏模式下的UI布局。
1、在layout文件夹下面的main.xml文件:
[html]
view plaincopyprint?
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
2、在layout-land文件夹下面的main.xml文件,注意,它比上面的代码多了两个Button视图:
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- 新增加的两个Button -->
<Button
android:id="@+id/button6"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Top Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button7"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Bottom Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- 新增加的两个Button -->
<Button
android:id="@+id/button6"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Top Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button7"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Bottom Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
3、当这个activity在竖屏模式下的时候,只显示5个按钮。
4、当在横屏模式下,将会显示7个按钮,这也就说明了,在不同的屏幕方向的模式下,将会加载不同的布局文件。
5、使用这种方法,当设备的方向改变,Android会自动地加载合适的布局文件,去适应屏幕的方向。
示例图:
基本上,在layout文件夹下面的main.xml定义了在portrait竖屏模式下activity的布局。但在layyout-land文件夹下面的main.xml定义了横屏模式下的UI布局。
1、在layout文件夹下面的main.xml文件:
[html]
view plaincopyprint?
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
[html] view plaincopyprint? <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top Left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top Right" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Left" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Right" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" /> <Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Middle" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top Left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top Right" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Left" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom Right" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" /> <Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Middle" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
2、在layout-land文件夹下面的main.xml文件,注意,它比上面的代码多了两个Button视图:
[html]
view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- 新增加的两个Button -->
<Button
android:id="@+id/button6"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Top Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button7"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Bottom Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- 新增加的两个Button -->
<Button
android:id="@+id/button6"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Top Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button7"
android:layout_width="180px"
android:layout_height="wrap_content"
android:text="Bottom Middle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
3、当这个activity在竖屏模式下的时候,只显示5个按钮。
4、当在横屏模式下,将会显示7个按钮,这也就说明了,在不同的屏幕方向的模式下,将会加载不同的布局文件。
5、使用这种方法,当设备的方向改变,Android会自动地加载合适的布局文件,去适应屏幕的方向。
相关文章推荐
- Android 程式开发:(七)适应屏幕的方向 —— 7.2“锚定”视图
- Android 程式开发:(八)处理屏幕方向的变化 —— 8.4控制Activity的显示方向
- Android 程式开发:(八)处理屏幕方向的变化 —— 8.2在“变化”中保存状态信息
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- Android程序如何自适应屏幕的方向和大小
- 微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应
- Android开发之改动屏幕方向
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- Android开发之修改屏幕方向
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- android获取手机屏幕尺寸和旋转方向
- android游戏开发适应多屏幕的问题 .
- Android开发全程记录(八)——设置ImageView显示的图片铺满全屏(适应魅族等不常见屏幕比例)
- Android开发之屏幕大小自适应
- Android多屏幕尺寸开发要点
- Android开发_获取手机屏幕的尺寸
- Android 程式开发:(五)屏幕组件 —— 5.7 ScrollView滚动视图
- Android开发-触屏事件的获取和触摸屏幕位置