未添加自定义视图层次结构子级 [英] Custom view hierarchy child not added

查看:85
本文介绍了未添加自定义视图层次结构子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图层次结构,如下所示:

I have a hierarchy of custom views that looks like this:

Activity(RelativeLayout)-> ParentLayout(FrameLayout)-> ChildLayout(LinearLayout)

Activity(RelativeLayout) -> ParentLayout(FrameLayout) -> ChildLayout(LinearLayout)

活动和父级布局已添加并显示得很好,但子级却没有.我已经在设备监视器中查看了层次结构查看器,以确认它没有被添加到视图层次结构中.

The activity and parent layout are added and displayed just fine, but the child is not. I have looked at the hierarchy viewer in the device monitor to confirm it is not being added to the view hierarchy.

我实际上要做的就是创建一个视图层次结构,这样我就可以在视图的各个位置处理触摸事件.

Really all I'm trying to do here is create a view hierarchy so I can play around with handling touch events at various places in the view.

这就是一切:

main_activity.xml

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <net.openeye.touchevents.ParentLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#558833" />

</RelativeLayout>

parent_layout.xml:

parent_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<net.openeye.touchevents.ParentLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <net.openeye.touchevents.ChildLayout
        android:id="@+id/child_view"
        android:layout_width="300dp"
        android:layout_height="300dp" />

</net.openeye.touchevents.ParentLayout>

ParentLayout.java:

ParentLayout.java:

public class ParentLayout extends FrameLayout implements View.OnTouchListener {
    public ParentLayout(Context context) {
        super(context);
    }

    public ParentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ParentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

child_layout.xml

child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<net.openeye.touchevents.ChildLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:background="#0066dd"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hi"/>

</net.openeye.touchevents.ChildLayout>

ChildLayout.java:

ChildLayout.java:

public class ChildLayout extends LinearLayout {
    public ChildLayout(Context context) {
        super(context);
    }

    public ChildLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChildLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

我想念什么?我还有另一个基本上以相同方式设置的项目,除了子视图是动态膨胀和添加的,而不是直接添加到xml布局文件中的.似乎应该可以使用,但我不明白为什么不可以.

What am I missing? I have another project that is basically set up the same way, except the child views are dynamically inflated and added, instead of being directly added in the xml layout files. This seems like it should work and I don't understand why it doesn't.

推荐答案

因此,当您拥有自定义视图类时,您不想让布局文件的视图与自定义类具有相同的类型.即,如果我有ParentLayout.java,则我不希望parent_layout.xml的根为<net.openeye.TouchEvents.ParentLayout>.看起来,当您同时需要自定义布局文件和自定义视图类时,需要使视图类对布局进行膨胀.如果布局具有与类相同的元素(在本例中为根),则在视图使布局膨胀时,它将导致无限递归,这将实例化类,从而使布局膨胀...依此类推.

So it looks like when you have a custom view class, you don't want to have the view of the layout file be the same type as the custom class. i.e., if I have ParentLayout.java, I don't want parent_layout.xml's root to be <net.openeye.TouchEvents.ParentLayout>. It seems that when you want both a custom layout file and custom view class, you need to have the view class inflate the layout. If the layout has an element (the root, on this case) that is the same as the class, it will cause infinite recursion as the view inflates the layout, which instantiates the class, which inflates the layout... and so on.

通过进行以下更改,我终于可以正常工作了:

I got this to work finally by making the following changes:

parent_layout.xml:
将根元素从net.openeye.TouchEvents.ParentLayout更改为其扩展的类FrameLayout.现在看起来像这样:

parent_layout.xml:
Change the root element from net.openeye.TouchEvents.ParentLayout to the class it extends, FrameLayout. It now looks like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- ... -->
</FrameLayout>

child_layout.xml:
将根元素从net.openeye.TouchEvents.ChildLayout更改为其扩展的类LinearLayout.现在看起来像这样:

child_layout.xml:
Change the root element from net.openeye.TouchEvents.ChildLayout to the class it extends, LinearLayout. It now looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:orientation="vertical">
    <!-- ... -->
</LinearLayout>

ParentLayout.java:在实例化期间膨胀其布局.现在看起来像这样:

ParentLayout.java: Inflate it's layout during instantiation. It now looks like this:

public ParentLayout(Context context) {
    super(context);
    init(context);
}

public ParentLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public ParentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

private void init(Context context) {
    inflate(context, R.layout.parent_layout, this);
}

ChildLayout.java:与ParentLayout.java相同,只是将child_layout充气.

ChildLayout.java: Same thing as ParentLayout.java, but inflate child_layout.

在开始工作并考虑了为什么会发生这种情况之后,就知道它是如何工作的.

After getting this working and thinking about why this is happening, it makes sense that this is how it works.

这篇关于未添加自定义视图层次结构子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆