您的位置:首页 > 移动开发 > Android开发

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"?>

[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会自动地加载合适的布局文件,去适应屏幕的方向。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: