我一直在开发一个应用程序,以在 Android 上着手。我遇到了这样的问题,即当我的布局转向其他方向时无法正确显示:
这是我的垂直布局代码:
<?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
。如果你在这里阅读:
它讨论了三个可能的值以及它们的作用。
fill_parent: View 应与其父 View 一样大(减去填充)。这个常量从 API 级别 8 开始被弃用,被 match_parent 取代。
match_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/