【问题标题】:Xml layout changes while changing orientationXml 布局在更改方向时更改
【发布时间】:2012-03-09 08:26:32
【问题描述】:

我有两个活动取决于SD 卡的存在情况。一个活动有三个Buttons 和一个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 文件。
  • 请发布您的 xml 文件。如果您需要根据方向更改布局,则必须为 layout-port 和 layout-land 设置两个布局。
  • 您是否尝试过为纵向/横向提供不同的布局?
  • 怎么做?并且两者都有确切的副本还是会有所不同???

标签: android xml layout orientation


【解决方案1】:

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

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

如需进一步参考,您可以查看: http://developer.android.com/guide/practices/screens_support.html

【讨论】:

  • fine :) 我的另一个问题是如何使用布局来支持不同的屏幕尺寸.....我正在做的是每次我重新排列和运行应用程序并检查我的设备但如果我在不同的应用程序中运行相同,它就不一样.....
  • 两个文件名必须相同。
【解决方案2】:

您可以在项目中创建一个名为“layout-land”的文件夹(在“res”目录中)。在那里,您可以从普通的“布局”文件夹中复制所有 xml 样式,并将它们重新设置为横向模式。 Android 会自动将这些布局用于横向模式。

您的项目将如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多