layoutinflater - 父级为空

发布于 2024-11-02 04:43:18 字数 2703 浏览 1 评论 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>

使用 merge 标签并使用 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 函数!!!我的错之一

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="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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七分※倦醒 2024-11-09 04:43:18

你应该尝试:
LinearLayout视图 = (LinearLayout)inflater.inflate(R.layout.titlebar, null, false);

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

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.

音栖息无 2024-11-09 04:43:18

则应该尝试

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 文件的根。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文