【发布时间】:2013-09-18 02:45:34
【问题描述】:
经过对 SO 和 google 的大量研究,我还没有看到任何人遇到与我遇到的完全相同的问题,所以这里是:
我最近在一个 Android 应用上重做了整个 UI。在大多数情况下,我只对每个屏幕进行了外观更改。它们按预期完美地出现在 Eclipse 的 UI 编辑器中。但是,执行此操作后,所有测试设备和模拟器上的两个屏幕都停止正确布局。
现在,这两个屏幕的一个大问题是根级 LinearLayout 似乎没有真正尊重 layout_height 或 layout_width 的 fill_parent。看起来它正在被测量,就好像它被设置为 wrap_content 一样。它只占用了大约 70% 的屏幕——这足以将单个元素包裹在根 LinearLayout 中。我会发布一张图片,但作为一个新用户,我不被允许。
布局未拉伸以填满屏幕。这是布局的代码,除了LinearLayouts中还有一些包含TextView和EditText。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000"
android:orientation="vertical" >
<TextView
style="@style/sans.white.16.bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:text="@string/edit_account" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.teamunify.ondeck.widget.TUTextView
style="@style/sans.white.14"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/first_name" />
<com.teamunify.ondeck.widget.TUEditText
android:id="@+id/acc_editor_first"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:selectAllOnFocus="true"
android:singleLine="true" />
</LinearLayout>
我认为根 LinearLayout 应该填充高度和宽度。我在我们的应用程序中多次使用相同的布局,没有问题。 (快速统计一下,我在我们的应用程序中使用了 76 个 LinearLayouts,除了其中两个之外,所有这些都可以正常工作。)
起初,我怀疑我们的自定义类度量可能会破坏一些东西,所以我将布局更改为使用所有普通的 EditText,但没有任何变化。我仔细检查了活动,但除了加载此 xml 之外,它什么也没做。所以,无奈之下,我重新设计了这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000"
android:orientation="vertical" >
<TextView
style="@style/sans.white.16.bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:paddingTop="4dp"
android:text="@string/edit_account" />
</LinearLayout>
之后,“编辑帐户”字样出现,左上角带有黑色背景。显然,LinearLayout 没有填充父级。
长话短说,我在问如何解决这个问题,以便 LinearLayout 按预期填满屏幕。
我完全不知道为什么会发生这种情况,我当然希望 SO 上的某个人有一个想法。这让我把头发拔掉了!
【问题讨论】:
-
我在使用布局权重时看到了这个问题。我的解决方案是将布局的长度强制为 0,然后一切都神奇地起作用了。 Android 的某个地方肯定存在错误
-
Gabe,我不太确定我明白你在说什么。能详细点吗?