0

我有延伸的课程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 功能!!!我的错误之一

4

2 回答 2

0

你应该试试

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);

现在您可以在任何需要的地方添加此标题栏。

更新

这是 inflate 方法的 apidocs:

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

自:API 级别 1
从指定的 xml 资源扩充新的视图层次结构。如果有错误,则抛出 InflateException。

参数 用于加载的 XML 布局资源的
资源ID(例如,R.layout.main_page)
root 可选视图作为生成的层次结构的父级(如果 attachToRoot 为真),或者只是提供一组 LayoutParams 值的对象返回层次结构的根(如果 attachToRoot 为 false。)
attachToRoot 膨胀的层次结构是否应附加到根参数?如果为 false,则 root 仅用于为 XML 中的根视图创建正确的 LayoutParams 子类。

返回 膨胀层次结构的根视图。如果提供了 root 并且 attachToRoot 为真,则这是 root;否则它是膨胀的 XML 文件的根。

于 2011-04-15T08:57:59.650 回答
0

您应该尝试: LinearLayout view = (LinearLayout)inflater.inflate(R.layout.titlebar, null, false);

AFAIK 这将返回视图,其根是膨胀 XML 的根。

于 2011-04-15T08:26:28.070 回答