【Android(安卓)开发教程】重新布局
16lz
2021-01-25
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/如果想根据屏幕的方向自定义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 plain copy- <prename="code"class="html"><?xmlversion="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="TopLeft"
- 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="TopRight"
- 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="BottomLeft"
- 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="BottomRight"
- 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>
- <?xmlversion="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="TopLeft"
- 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="TopRight"
- 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="BottomLeft"
- 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="BottomRight"
- 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="TopMiddle"
- 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="BottomMiddle"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:layout_alignParentBottom="true"/>
- </RelativeLayout>
4. 当在横屏模式下,将会显示7个按钮,这也就说明了,在不同的屏幕方向的模式下,将会加载不同的布局文件。
更多相关文章
- Mac Android(安卓)Studio处理unable to access android sdk add-
- Android(安卓)HttpURLConnection连接服务器异常
- android 组件隐蔽显示状态
- Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控
- android中LayoutAnimationController类的使用
- 浅析Android(安卓)Dialog中setContentView()方法
- Android(安卓)ListView单选CheckBox
- [置顶] Android常用UI控件之PopupWindow
- Android(安卓)自定义View (一)