getChildAt()在Custom Viewgroup构造函数中不起作用

时间:2015-01-27 11:58:06

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

我正在创建一个像化合物一样的视图。这是 like_layout_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/like" />
    <TextView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="12569"
        android:textSize="20px" />
</LinearLayout>

这是 LikeView 类扩展 LinearLayout

public class LikeView extends LinearLayout {

    private ImageView likeImageView;
    private TextView likeCountTextView;

    private boolean isLiked;
    private long likeCount;

    public LikeView(Context context) {
        this(context, null);
    }

    public LikeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }
    public LikeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.like_layout_1, this);

        likeImageView = (ImageView) getChildAt(0);
        likeCountTextView = (TextView) getChildAt(1);
    }
}
由于这两行,发生

通胀异常

        likeImageView = (ImageView) getChildAt(0);
        likeCountTextView = (TextView) getChildAt(1);

方法 getChildAt()会产生问题,因为如果我对这两行进行注释,则不会发生异常并且工作正常。

注意:我不想使用 findByView()方法访问孩子。

3 个答案:

答案 0 :(得分:1)

  

我正在创建像视图一样的复合

您是否尝试过[android:drawableLeft][1][android:drawableRight][2]

例如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1" android:text="12569"
        android:layout_width="wrap_content" android:drawableLeft="@drawable/like"
        android:layout_height="wrap_content"
        android:gravity="center" />

</LinearLayout>

现在正在努力。

在构造函数中,您无法获取子视图实例,因为在父完全初始化之前它不会初始化。所以检查addView方法

如果您通过扩展LinearLayout LikeView而不是在xml布局中创建自定义视图,那么它应该与包含LikeView自定义控件类的包名一起使用

  

不喜欢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/like" />
    <TextView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="12569"
        android:textSize="20px" />
</LinearLayout>
  

应该

<?xml version="1.0" encoding="utf-8"?>
<com.yourpackage.LikeView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/like" />
    <TextView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="12569"
        android:textSize="20px" />
</com.yourpackage.LikeView>

LikeView.java

  

为10-20岁以上的儿童控制手段提出质疑

    package com.yourpackage;

    import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;



public class LikeView extends LinearLayout {

    private ImageView likeImageView;
    private TextView likeCountTextView;

    private boolean isLiked;
    private long likeCount;

    public LikeView(Context context) {
        this(context, null);
    }

    public LikeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public LikeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.ViewGroup#addView(android.view.View, int,
     * android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        // If the view hierarchy is fixed and limitted thn you can also check
        // with the index

        // but from constructor you wont get instance because first parent will
        // be loaded and child may not be loaded

        int id = child.getId();
        if (id == 0)
            return;
        switch (id) {
        case R.id.seekbar:
            // cast here with seekbar
            break;
        case R.id.txtTouchEventTest:
            likeCountTextView = (TextView) child;
            break;
        case R.id.imageView:
            likeImageView = (ImageView) child;
            break;

        case R.id.show_notification:
            // cast here with button
            break;
        case R.id.hide_notification:
            // cast here with button
            break;
        }

    }

}
  

已编辑:未在课堂上使用或导入ID或应用程序R

循环和你即兴发挥的所有逻辑

@Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        // If the view hierarchy is fixed and limitted thn you can also check
        // with the index

        // but from constructor you wont get instance because first parent will
        // be loaded and child may not be loaded

        // TO handle it from index

        int size = getChildCount();
        for (int i = 0; i < size; i++) {
            switch (i) {
            case 0:
                if (TextView.class.isInstance(getChildAt(0)))
                    likeCountTextView = (TextView) getChildAt(0);
                break;
            case 1:
                if (TextView.class.isInstance(getChildAt(1)))
                    likeCountTextView = (TextView) getChildAt(1);
                break;
            case 2:
                if (SeekBar.class.isInstance(child)) {
                    SeekBar seekbar = (SeekBar) child;
                }
                break;
            case 3:

                if (ImageView.class.isInstance(child)) {
                    likeImageView = (ImageView) child;
                }

                break;
            case 4:
                if (Button.class.isInstance(child)) {
                    Button btnFirst = (Button) child;
                }

                break;
            case 5:
                if (Button.class.isInstance(child)) {
                    Button btnSecond = (Button) child;
                }
                break;
            }

        }

        // int id = child.getId();
        // if (id == 0)
        // return;
        // switch (id) {
        // case R.id.seekbar:
        // // cast here with seekbar
        // break;
        // case R.id.txtTouchEventTest:
        // likeCountTextView = (TextView) child;
        // break;
        // case R.id.imageView:
        // likeImageView = (ImageView) child;
        // break;
        //
        // case R.id.show_notification:
        // // cast here with button
        // break;
        // case R.id.hide_notification:
        // // cast here with button
        // break;
        // }

    }

我用过的XML文件。

<?xml version="1.0" encoding="utf-8"?>
<com.hiddenbrains.lib.emailutility.LikeView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtTouchEventTest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:padding="20dp"
        android:text="Hello World"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/textViewSecond"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:padding="20dp"
        android:text="TextView Second"
        android:textColor="@android:color/black" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:max="100" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/show_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/hide_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

</com.hiddenbrains.lib.emailutility.LikeView>

检查this以了解控件的自定义

希望我能正确地回答你的问题..如果不是请告诉我。

答案 1 :(得分:1)

你可以试试这个

@Override
protected void onFinishInflate()
{
    super.onFinishInflate();
    likeImageView = (ImageView) getChildAt(0);
    likeCountTextView = (TextView) getChildAt(1);
}

答案 2 :(得分:0)

我遇到了同样的问题。我解决了如下:

    likeImageView = (ImageView) getChildAt(1);
    likeCountTextView = (TextView) getChildAt(2);

因为如果您执行getChildAt(0)getChildAt(1),它会将ImageView扩展为父级LinearLayout