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功能!!!我的错之一
没有找到相关结果
已邀请:
2 个回复
倾坞髓
挂帘妈乡
如果您在活动课堂中,或者
如果您处于活动类中的内联类中。 然后,如果要将新视图添加到活动中的其他视图中(例如,my_canvas):
现在,您可以在任何需要的地方添加此titleBar。 更新资料 这是inflate方法的apidocs:
由于: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文件的根。