我有一个菜单活动,其中有几个定位按钮。它在人像模式下非常适合我的手机屏幕,但在横向模式下它会被破坏。我知道,当我有固定的定位,这将是结果。有人能告诉我如何定位我的按钮,使我的活动也保持在横向模式下吗?非常感谢。
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bezpecnost_over"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/kurenie_over"
android:layout_marginLeft="200dp"
android:layout_marginTop="50dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/osvetlenie_over"
android:layout_marginLeft="200dp"
android:layout_marginTop="200dp"
android:onClick="obrOsv" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pohodlie_over"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
在人像模式下,它非常适合我的手机屏幕,但在景观模式,它被破坏了。
这适用于基于相对的布局。开发人员在这种情况下通常会做什么:
- 在res下创建一个名为
layout-land
的文件夹 - 制作一个与纵向模式下的布局具有相同名称的xml(包含在
res/layout
中) - 横向模式的位置
由于xml文件名是相同的,因此当该活动设置内容视图时
setContentView (R.layout.my_layout);
将使用layout-land
文件夹中的my_layout.xml
文件。
另请阅读布局&支持Android文档中的多个屏幕,它们都有有用的信息,后者谈论不同的限定符(如layout-land
)。
那么您是在使用相对布局,通过增加边距来水平分隔按钮吗?这完全不理解布局是如何工作的。首先,将RelativeLayout替换为方向设置为水平的LinearLayout,并去掉所有边距——元素之间的边距应该只有几个像素。我建议你阅读关于布局如何工作的Android教程。