【问题标题】:Custom view hierarchy child not added未添加自定义视图层次结构子项
【发布时间】:2015-08-13 20:25:34
【问题描述】:

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

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

activity 和 parent 布局添加并显示得很好,但 child 不是。我查看了设备监视器中的层次结构查看器,以确认它没有被添加到视图层次结构中。

我真正要做的就是创建一个视图层次结构,这样我就可以在视图的不同位置处理触摸事件。

一切都在这里:

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:

<?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:

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

<?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:

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布局文件中。这似乎应该可以工作,但我不明白为什么它不起作用。

【问题讨论】:

    标签: android android-layout android-custom-view


    【解决方案1】:

    所以看起来当你有一个自定义视图类时,你不希望布局文件的视图与自定义类的类型相同。即,如果我有 ParentLayout.java,我不希望 parent_layout.xml 的根是 &lt;net.openeye.TouchEvents.ParentLayout&gt;。似乎当您想要自定义布局文件和自定义视图类时,您需要让视图类膨胀布局。如果布局有一个与类相同的元素(在这种情况下为根),它将导致无限递归,因为视图膨胀布局,实例化类,膨胀布局......等等。

    通过进行以下更改,我终于让它工作了:

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

    <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。现在看起来像这样:

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

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

    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

    在完成这项工作并思考为什么会发生这种情况之后,这就是它的工作原理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 2011-11-23
      • 2010-10-18
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多