【问题标题】:"Workspace" child views not filling height on Google "iosched" appGoogle“iosched”应用程序上的“工作区”子视图未填充高度
【发布时间】:2011-06-08 16:41:26
【问题描述】:

我正在使用来自 Google I/O 2011 的 iosched 应用程序。查看 ScheduleFragment,我正在测试 Workspace 子级的不同视图,用于来回翻转页面。但是,我无法让我的孩子的意见填补父母。我添加了几种背景颜色和一些填充以显示所有内容的布局。这是我的测试...

我像这样在 fragment_schedule.xml 中的“工作区”项中添加了红色背景色...

android:background="#cc0000"

然后,我没有使用 blocks_content.xml 作为子视图,而是创建了自己的 pending_content.xml,它只是一个绿色背景的 LinearLayout(高度为 fill_parent)和一个蓝色背景的 TextView(高度也为 fill_parent)用一个简单的文本行。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00cc00"
    android:padding="5dip"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView
        android:id="@+id/text_view"
        android:background="#0000cc"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="Pending Content"
        android:textColor="#ffffff"
        />
</LinearLayout>

我添加我的视图,就像 I/O 应用程序有“天”一样...

ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_schedule, null);
mWorkspace = (Workspace) root.findViewById(R.id.workspace);

ShootView view = new ShootView();
view.index = mViews.size();
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);
view.label = "Pending";
mWorkspace.addView(view.rootView);
mViews.add(view);

这是我在设备上查看时看到的内容:

您会看到红色的 Workspace 填充了可用的父空间,但绿色的 LinearLayout 没有。我错过了什么?

【问题讨论】:

  • 工作区本身是否有layout_height="fill_parent"
  • 是的,除了添加背景颜色之外,我没有更改该 XML。您可以看出 Workspace 正在填充父级,因为红色正在填充背景。除了添加背景色外,和... code.google.com/p/iosched/source/browse/android/res/layout/…
  • 如果你只是以编程方式给孩子一个新的 LayoutParams 会发生什么?
  • 我也可以试试。目前我正在使用我在您的回答中提到的方法(将 child.getMeasuredHeight 替换为 this.getMeasuredHeight)。

标签: android workspace fill-parent


【解决方案1】:

问题是在你的通胀阶段:

view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);

您丢失了 XML 中定义的 LayoutParams,您的视图实际上以默认布局参数结束。

您需要做的是提供root,并且在膨胀期间根本不将膨胀的布局附加到根:

view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, mWorkspace, false);

这将允许布局充气器根据已解析的 XML 属性为您的子视图构造一组适当的 LayoutParams

【讨论】:

  • 嗯。恐怕这行不通。我之前已经尝试过传递“mWorkspace”但没有最后一个参数。当然,之后尝试将视图添加到 mWorkspace 时,它​​给出了一个例外。这种方式(传递 false)不会给出异常,但它也不会拉伸 LinearLayout。还有其他想法吗?
  • 我想出了一个解决方案。我开始在 Workspace.java 中调试“onLayout”。在 child.layout 函数调用中,我将“child.getMeasuredHeight()”更改为“this.getMeasuredHeight()”以改用工作区的高度。对于比工作区高度短的页面以及大于工作区高度的子(长 ListView)的情况,似乎工作正常。
【解决方案2】:

我想出了一个解决方案。我开始在 Workspace.java 中调试onLayout。在 child.layout 函数调用中,我将 child.getMeasuredHeight() 更改为 this.getMeasuredHeight() 以改用工作区的高度。

对于比工作区高度短的页面以及大于工作区高度的子(长 ListView)的情况,似乎可以正常工作。

【讨论】:

    猜你喜欢
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多