【问题标题】:How to correct the horizontal orientation for my Android app?如何纠正我的 Android 应用程序的水平方向?
【发布时间】:2014-02-20 05:25:53
【问题描述】:

我已经设置了如下布局,它是垂直格式,当我将它更改为水平方向时,它完全搞砸了。

但是当我改变设备的方向时,它变成了这样:

谁能帮我解决这个问题?

以下的XML代码如下:

    <?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:gravity="center" >

    <Button
        android:id="@+id/bBackfromInquiry"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/back" />

    <Button
        android:id="@+id/bSubmit"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/mail_us" />

    <EditText
        android:id="@+id/etMailbody"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_above="@+id/bSubmit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="@string/inqhintComments" />

    <EditText
        android:id="@+id/etPhone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="27dp"
        android:ems="10"
        android:hint="@string/inqhintPhone"
        android:inputType="phone" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bBackfromInquiry"
        android:layout_alignBottom="@+id/bBackfromInquiry"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/bBackfromInquiry"
        android:text="@string/buttonInquiry"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/inqComments"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/bBackfromInquiry"
        android:ems="10"
        android:hint="@string/inqhintName"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/etName"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/inqhintEmail"
        android:inputType="textEmailAddress" />

</RelativeLayout>

【问题讨论】:

  • 请发布纵向和横向模式的 xml。
  • XML 文件是相同的,我没有为同一个活动创建不同的 xml,如果可以,请告诉我,但是我要发布现有的 xml,请稍等。
  • 发布你的 xml 代码@MananGupta
  • @kaushik - 我已经添加了xml代码,请检查
  • @18446744073709551615 - 我已添加 XML 文件,请查看

标签: android android-relativelayout android-orientation


【解决方案1】:

在您的设备中尝试此 xml 代码

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sc"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/bBackfromInquiry"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/back" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/buttonInquiry"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <EditText
            android:id="@+id/etName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="@string/inqhintName"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/etEmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="@string/inqhintEmail"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/etPhone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="@string/inqhintPhone"
            android:inputType="phone" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginTop="15dp"
            android:text="@string/inqComments"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/etMailbody"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_gravity="left"
            android:layout_marginTop="15dp"
            android:hint="@string/inqhintComments" />

        <Button
            android:id="@+id/bSubmit"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_gravity="right"
            android:layout_marginTop="15dp"
            android:background="@drawable/mail_us" />
    </LinearLayout>

</ScrollView>

【讨论】:

  • 谢谢,实施后我会尽快回复
【解决方案2】:

我的建议是在 res/layout-land 这样的 res 目录中为横向创建另一个 xml,以便在横向模式下获得完美的视图 或者您可以将滚动视图放在 xml 中

【讨论】:

  • 好的,我明白了。但是如何在java文件中实现相同的。意味着我仍然不清楚如何以相同的布局在旋转时以不同的方向加载的方式连接 xml。以及如何将一个 java 代码类与两个布局连接起来。如果你知道我的意思。
  • 它会自动重新加载 xml 文件,主要是你应该在布局文件夹中放置相同的 xml 名称,就像你在布局文件夹中有 main.xml 一样,而不是在 res/layout-land 中放置相同的文件名/main.xml 如果设备配置更改,则在运行时 android 如果设备处于横向模式,则从横向文件夹中拾取 xml
  • 或者如果您想要手动处理横向或 portatit 模式,请参考此链接stackoverflow.com/questions/13411391/…
【解决方案3】:

有时当你省略时会发生这样的事情

android:layout_below="@id/xyz"

但在这种情况下,我认为屏幕上的所有字段都没有足够的空间。 我认为 ScrollView(可能带有垂直 LinearLayout)可以帮助您。

更新(在 XML 发布后):

尝试仅使用layout_below 重写它。目前看起来一个视图在顶部,第二个在它下面,第三个在中间,但不能保证第二个和第三个视图不会重叠。

【讨论】:

  • 好的,谢谢回复,我会考虑的,我已经添加了xml代码,检查一下。
【解决方案4】:

@MananGupta : 我建议你也为横向模式创建 xml....

  1. 在您的资源中创建一个文件夹:-

res / layout-land

并在其下添加xml文件...

但请确保 xml 文件的名称与纵向模式下的名称相同,因为将引用相同的名称,但用于不同的模式。

欲了解更多信息,请访问此Link

【讨论】:

    【解决方案5】:

    创建一个 layout-land 目录,并将布局 XML 文件的横向版本放在该目录中。 此处提供的完整信息

    http://developer.android.com/guide/practices/screens_support.html

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 2016-01-01
      相关资源
      最近更新 更多