开发者

layoutinflater - parent is null

I have class which extends LinearLayout

in constructor I call

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, this, true);

I can access Views in this view but it is not drawn. I think the reason is that mParent of this view is still null. But why? the Partent should be this (the class which extends LinearLayout)

here the xml:

<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="35px" 
android:layout_width="fill_parent"
android:background="@drawable/titlebar_bg">

<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fi开发者_如何学Goll_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView></RelativeLayout>

SOLUTION

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:text="test" 
    android:id="@+id/title" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10pt" 
    android:layout_height="wrap_content"
    android:textSize="10pt"
    android:layout_marginTop="3dip"></TextView>

<TextView android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:text="10:09 AM" 
    android:id="@+id/time" 
    android:textColor="#000000"
    android:layout_alignParentRight="true" 
    android:layout_marginRight="5px"
    android:layout_marginTop="3px"
    android:textSize="10pt"></TextView>

using the merge tag and use the xml tags.

<package.Titlebar 
            android:id="@+id/testtitlebar" 
            android:layout_height="35px" 
            android:layout_width="fill_parent"
            android:background="@drawable/titlebar_bg"></package.Titlebar>

this is how it looks in the class:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.titlebar, this);

no onLayout function!!! One of my faults


You should try : LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, null, false);

AFAIK this will return view, which root is the root of the inflated XML.


You should try

LayoutInflater inflater = LayoutInflater.from(this);

if you are inside your activity class, or

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);

if you are in an inline class inside your activity class.

Then if you want to add the new view to an other inside your activity (say my_canvas):

//[YourActivity.] is needed when you are in an inline class!
//RelativeLayout is not mandatory, you can have any other layout there
final RelativeLayout canvas = (RelativeLayout) [YourActivity.]this.findViewById(R.id.my_canvas);
final View titleBar = inflater.inflate(R.layout.titlebar, canvas, false);

Now you can add this titleBar wherever you need.

Update

This is the apidocs for the inflate method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

Since: API Level 1
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜