Android 适配横屏

来源:互联网 发布:教室楼图纸软件 编辑:程序博客网 时间:2024/04/30 02:26

今天书看到Android中横屏的适配。如图,在横屏模式下屏幕布局发生了偏差。


可以看出横屏模式下右侧有明显的空间。


为适配横屏有两种方式,一种是使用相对布局,以屏幕边界作为参照点进行布局。先上图。



布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <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_alignParentRight="true"        android:layout_alignParentTop="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="wrap_content"        android:layout_height="wrap_content"        android:text="Middle"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"/></RelativeLayout>


还有一种方法比较灵活,可以更好的使用横屏下的屏幕布局。

另一种方法是在resource文件夹下新建layout_land文件夹,然后新建一个与竖屏下布局文件文件名相同的布局文件。

layout_land/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <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_alignParentRight="true"        android:layout_alignParentTop="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="match_parent"        android:layout_height="wrap_content"        android:text="Middle"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"/>    <Button        android:id="@+id/button6"        android:layout_width="180dp"        android:layout_height="wrap_content"        android:text="Top Middle"        android:layout_centerHorizontal="true"        android:layout_alignParentTop="true"/>    <Button        android:id="@+id/button7"        android:layout_width="180dp"        android:layout_height="wrap_content"        android:text="Bottom Middle"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"/></RelativeLayout>

最后上图。


0 0
原创粉丝点击