layoutinflater-parent为null

| 我的课扩展了
LinearLayout
在构造函数中,我称
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, this, true);
我可以在此视图中访问“视图”,但未绘制。我认为原因是该视图的mParent仍然为空。但为什么? Partent应该是这个(扩展LinearLayout的类) 这里的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=\"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></RelativeLayout>
<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>
使用合并标签并使用xml标签。
<package.Titlebar 
            android:id=\"@+id/testtitlebar\" 
            android:layout_height=\"35px\" 
            android:layout_width=\"fill_parent\"
            android:background=\"@drawable/titlebar_bg\"></package.Titlebar>
这是在类中的外观:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.titlebar, this);
没有onLayout功能!!!我的错之一     
已邀请:
你应该试试 : LinearLayout视图=(LinearLayout)inflater.inflate(R.layout.titlebar,null,false); AFAIK这将返回视图,该根是膨胀的XML的根。     
你应该试试
LayoutInflater inflater = LayoutInflater.from(this);
如果您在活动课堂中,或者
LayoutInflater inflater = LayoutInflater.from(YourActivity.this);
如果您处于活动类中的内联类中。 然后,如果要将新视图添加到活动中的其他视图中(例如,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);
现在,您可以在任何需要的地方添加此titleBar。 更新资料 这是inflate方法的apidocs:
public View inflate (int resource, ViewGroup root, boolean attachToRoot) 
由于:API级别1 从指定的xml资源中添加新的视图层次结构。如果有错误,则抛出InflateException。 参量 要加载的XML布局资源的资源ID(例如R.layout.main_page) root可选视图,该视图是生成的层次结构的父级(如果attachToRoot为true),或者只是一个对象,该对象为返回的层次结构的根目录提供一组LayoutParams值(如果attachToRoot为false)。 attachToRoot是否应将膨胀的层次结构附加到根参数?如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类。 退货 膨胀层次结构的根视图。如果提供了root且attachToRoot为true,则为root;否则,它是膨胀的XML文件的根。     

要回复问题请先登录注册