2012-03-09 155 views
6

我有两个活动,根据SD卡的存在情况打开。一个活动有三个Buttons和一个TextView和另一个ImageView,一个按钮和缩放控制。当我改变方向时,这些按钮会在水平方向乱翻。如何解决这个问题?更改方向时更改Xml布局

我的“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> 
+0

请告诉我们您的布局XML文件(S)。 – 2012-03-09 08:30:26

+1

请发布您的xml文件。如果您需要根据方向更改布局,则必须为layout-port和layout-land设置两个布局。 – deepa 2012-03-09 08:31:56

+1

您是否尝试过为纵向/横向提供不同的布局? – Luksprog 2012-03-09 08:32:30

回答

8

您可以在您的项目称为“布局 - 土地”的文件夹(在“资源”目录)。在那里,你可以从正常的“布局”文件夹复制所有的XML样式,并重新设置它们为横向模式。 Android会自动将这些布局用于横向模式。

您的项目应该是这样的:

enter image description here

13

你必须创造一个纵向和横向模式单独的XML文件,并将其放置在不同的目录中。该设备将自动选择正确的一个。您可以使用下面的目录结构

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

有关进一步的参考,您可以检查: http://developer.android.com/guide/practices/screens_support.html

+0

罚款:)我的另一个问题是如何使用布局来支持不同的屏幕大小.....我所做的是,每次我重新排列和运行应用程序,并检查我的设备,但它不一样,如果我在不同的应用程序中运行相同的程序..... – jxgn 2012-03-09 11:22:17

+0

两个文件名都必须相同。 – 2016-04-20 06:55:43