3

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:

http://www.milescrew.com/images/workspace.png

You'll see that the red Workspace fills the available parent space, but the green LinearLayout does not. What am I missing?

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
robertmiles3
  • 899
  • 10
  • 20
  • Does the Workspace itself have `layout_height="fill_parent"`? – Roman Nurik Jun 03 '11 at 16:21
  • Yes, I haven't changed that XML except for adding the background color. You can tell that the Workspace is filling the parent because the red is filling the background. Besides the background color added, it's the same as... http://code.google.com/p/iosched/source/browse/android/res/layout/fragment_schedule.xml – robertmiles3 Jun 03 '11 at 17:02
  • What happens if you simply give the child a new LayoutParams programatically? – Roman Nurik Jun 04 '11 at 02:50
  • I can try that as well. Currently I'm using the method that I mentioned on your answer (replace child.getMeasuredHeight with this.getMeasuredHeight). – robertmiles3 Jun 06 '11 at 23:55

2 Answers2

3

The problem is that during your inflation step:

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

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:

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

This will allow the layout inflater to construct an appropriate set of LayoutParams for your child view from the parsed XML attributes.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
  • Hmm. I'm afraid that doesn't work. I had already previously tried to pass the "mWorkspace" but without the last parameter. Of course, it gave an exception when it tried to add the view to the mWorkspace after that. This way (passing false) doesn't give the exception, but it doesn't stretch out the LinearLayout either. Any other ideas? – robertmiles3 Jun 03 '11 at 19:05
  • I figured out a solution. I started debugging the "onLayout" in Workspace.java. In the child.layout function call, I changed "child.getMeasuredHeight()" to "this.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. – robertmiles3 Jun 03 '11 at 19:46
0

I figured out a solution. I started debugging the onLayout in Workspace.java. In the child.layout function call, I changed child.getMeasuredHeight() to this.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.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
robertmiles3
  • 899
  • 10
  • 20