“工作区” Google“iosched”上的子视图未填充高度应用程序
我正在使用 Google I/O 2011 上的 iosched 应用程序。查看 ScheduleFragment ,我正在 工作区 子项,用于来回翻页。然而,我很难让我的孩子的观点来填补父母的意见。我添加了几种背景颜色和一些填充来显示所有内容的布局位置。这是我的测试...
我向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);
这是我在设备上查看它时看到的内容:
您将看到红色工作区填充了可用的父空间,但绿色 LinearLayout 没有填充。我缺少什么?
I'm playing around with the iosched app from Google I/O 2011. Looking at the ScheduleFragment, I'm testing a different view on the Workspace children for flinging the pages back and forth. However, I'm having trouble getting my child views to fill the parent. I added several background colors and some padding to show where everything is laying out. Here's my test...
I added a red background color to the "Workspace" item in fragment_schedule.xml like this...
android:background="#cc0000"
Then, instead of using blocks_content.xml for the child view, I created my own pending_content.xml that is simply a green-background LinearLayout (with fill_parent for height) and a blue-background TextView (also with fill_parent for height) with a simple text line.
<?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 add my view just like the I/O app has "days"...
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);
And here's what I see when I view it on the device:
You'll see that the red Workspace fills the available parent space, but the green LinearLayout does not. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,在您的膨胀步骤中:
您丢失了 XML 中定义的 LayoutParams ,并且您的视图实际上最终以默认布局参数结束。
您需要做的是提供一个
root
,并且在膨胀期间不要将膨胀的布局附加到根:这将允许布局膨胀器构造一组适当的
LayoutParams
您的子视图来自已解析的 XML 属性。The problem is that during your inflation step:
You lose the
LayoutParams
defined in your XML, and your view actually ends up with default layout parameters.What you need to do is provide a
root
and simply not attach the inflated layout to the root during inflation:This will allow the layout inflater to construct an appropriate set of
LayoutParams
for your child view from the parsed XML attributes.我想出了一个解决办法。我开始调试 Workspace.java 中的
onLayout
。在 child.layout 函数调用中,我将child.getMeasuredHeight()
更改为this.getMeasuredHeight()
以使用工作区的高度。对于比工作区高度短的页面以及比工作区高度大的子页面(长 ListView)的情况,似乎工作得很好。
I figured out a solution. I started debugging the
onLayout
in Workspace.java. In the child.layout function call, I changedchild.getMeasuredHeight()
tothis.getMeasuredHeight()
to use the height of the workspace instead.Seems to work fine for the case of a page that is shorter than the workspace height as well as a child that is bigger (a long ListView) that is larger than the height of the workspace.