欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android 程式开发:(六)适应方向 —— 6.3 重新布局

来源: 开发者 投稿于  被查看 39939 次 评论:93

Android 程式开发:(六)适应方向 —— 6.3 重新布局


如果想根据屏幕的方向自定义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 plaincopy
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> 
[html] view plaincopy
<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 plaincopy
<?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>    www.2cto.com

3、当这个activity在竖屏模式下的时候,只显示5个按钮。

\

4、当在横屏模式下,将会显示7个按钮,这也就说明了,在不同的屏幕方向的模式下,将会加载不同的布局文件。

\

5、使用这种方法,当设备的方向改变,Android会自动地加载合适的布局文件,去适应屏幕的方向


摘自 manoel的专栏

相关文章

    暂无相关文章

用户评论