更改方向时,XML布局发生变化

6

我有两个活动,根据SD卡的存在打开。一个活动有三个按钮和一个TextView,另一个活动有一个ImageView、一个按钮和缩放控件。当我改变方向时,按钮在水平方向上会乱跑。如何解决这个问题?

我的'SD'卡布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1E1E1E"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_print"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="44dp"
        android:background="@drawable/my_button"
        android:text="@string/print" />

    <TextView
        android:id="@+id/text_SDmissing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:text="@string/SDmissing"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_print"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="58dp"
        android:background="@drawable/my_button"
        android:text="@string/camera" />

    <Button
        android:id="@+id/button_insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_SDmissing"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:background="@drawable/my_button"
        android:text="@string/insert" />

</RelativeLayout>

请展示您的布局XML文件(或文件)。 - Ted Hopp
1
请发布您的XML文件。如果您需要根据方向更改布局,则必须为layout-port和layout-land设置两个布局。 - deepa
1
你尝试过为纵向/横向提供不同的布局吗? - user
如何做到这一点?它们会完全相同还是会有所不同? - jxgn
2个回答

13

您需要为纵向和横向模式创建单独的XML文件,并将其放置在不同的目录中。设备将自动选择正确的文件。您可以使用以下目录结构。

res/layout/my_layout.xml   
res/layout-land/layout.xml

您可以查看以下内容以获取进一步参考:http://developer.android.com/guide/practices/screens_support.html


我的另一个问题是如何使用布局来支持不同的屏幕尺寸......我所做的是每次重新排列并运行应用程序,然后在我的设备上检查,但如果我在不同的应用程序中运行相同的应用程序,则情况并非如此。 - jxgn
两个文件名必须相同。 - Hardik Joshi

7
你可以在项目中创建一个名为“layout-land”的文件夹(在“res”目录下)。在这个文件夹中,你可以复制所有常规“layout”文件夹中的xml样式,并对横屏模式进行重新设计。Android会自动使用这些布局来适应横屏模式。
你的项目将如下所示: enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接