android - 布局未正确调整以适应方向变化

标签 android xml

我一直在开发一个应用程序,以在 Android 上着手。我遇到了这样的问题,即当我的布局转向其他方向时无法正确显示:

http://imgur.com/a/aPe2p

这是我的垂直布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="3dip" >

    <EditText
        android:id="@+id/passText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:hint="Passcode"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/messageText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="top|left"
        android:hint="Message" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/encodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Encode" />

        <Button
            android:id="@+id/decodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Decode" />
    </LinearLayout>

</LinearLayout>

这是我的横向布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dip" >

    <EditText
        android:id="@+id/passText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:hint="Passcode"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/messageText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="top|left"
        android:hint="Message" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/encodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Encode" />

        <Button
            android:id="@+id/decodeButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:text="Decode" />
    </LinearLayout>

</LinearLayout>

最佳答案

尝试将展开/缩小的项目的 android:layout_height 设置为 wrap_content。如果你在这里阅读:

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#attr_android:layout_height

它讨论了三个可能的值以及它们的作用。

fill_parent: View 应与其父 View 一样大(减去填充)。这个常量从 API 级别 8 开始被弃用,被 match_parent 取代。

ma​​tch_parent: View 应与其父 View 一样大(减去填充)。在 API 级别 8 中引入。

wrap_content: View 的大小应仅能容纳其内容(加上填充)。

您应该在其中几个项目上使用 wrap_content。下面我添加了一个示例,说明我会为您的垂直布局更改哪些高度:

<EditText
    android:id="@+id/passText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:hint="Passcode"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/messageText"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="top|left"
    android:hint="Message" >

    <requestFocus />
</EditText>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/encodeButton"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:text="Encode" />

    <Button
        android:id="@+id/decodeButton"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:text="Decode" />
</LinearLayout>

关于android - 布局未正确调整以适应方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507022/

相关文章:

Android:以编程方式更改时间的锁定/卡住布局?

java - 基本的 XML 到 HTML 转换

python - 如何为我的 Selenium 网页抓取找到更具描述性的 XML 路径?

android - sqlite首先按WHERE排序

java - 我的应用程序在启动时崩溃,我不确定为什么

android - 如何在 android 中使用我的 Asynctask

mysql - 计算 Extract Value 找到匹配项的行数

java - Kafka Producer 将消息从 XML 文件发布到 Kafka Topic

java - 如何在 java 中将 org.w3c.dom.Element 输出为字符串格式?

android - 无法安装 APK 托管我自己的 apache 服务器