开发者

"Workspace" child views not filling height on Google "iosched" app

开发者 https://www.devze.com 2023-03-11 09:21 出处:网络
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 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 th开发者_运维百科at 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:

"Workspace" child views not filling height on Google "iosched" app

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消