我正在使用来自Google I / O 2011的iosched应用。查看ScheduleFragment,我正在测试Workspace子项的不同视图,用于来回翻页。但是,我无法获取子视图以填充父级。我添加了几种背景颜色和一些填充,以显示所有内容的布局。这是我的测试...
我在fragment_schedule.xml中为“Workspace”项添加了一个红色背景颜色,就像这样......
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不会。我错过了什么?
答案 0 :(得分:3)
问题在于你的通胀步骤:
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);
您丢失了XML中定义的LayoutParams
,您的视图实际上以默认布局参数结束。
您需要做的是提供root
,并且在通胀期间根本不将膨胀的布局附加到根目录:
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, mWorkspace, false);
这将允许布局inflater根据解析的XML属性为您的子视图构造一组适当的LayoutParams
。
答案 1 :(得分:0)
我找到了解决方案。我开始调试Workspace.java中的onLayout
。在child.layout函数调用中,我将child.getMeasuredHeight()
更改为this.getMeasuredHeight()
以改为使用工作区的高度。
对于比工作空间高度短的页面以及比工作区高度更大的子项(长ListView),似乎工作正常。