自定义视图-如何设置他们的ID's,以便可以通过findViewById()找到它们



因此,我在代码中设置了一个自定义视图(没有为其定义XML布局),并想知道如何正确定义子视图ID。我有一个xml文件,定义与此类似的id。。但至少我是怎么理解的,因为我没有xml布局,所以没有AttribSet可以传递。。所以我的构造函数都只是(上下文上下文)类型。

<resources>
    <item type="id" name="textview1"/>
    <item type="id" name="textview2"/>
</resources>

我的观点大致如下:

public class MyView extends View {
    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;
    public MyView(Context context) {
        super(context);
        LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        setLayoutParams(fill);
        Resources res = getResources();
        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(fill);
        textView1 = new TextView(context);
        textView1.setId(R.id.textview1);
        // other init stuff here
        textView2 = new TextView(context);
        textView2.setId(R.id.textview2);
        // other init stuff here
        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // *snip lots of stuff*
        baseLayout.draw(canvas);
    }
    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView1() {
        return textView1;
    }
    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView2() {
        return textView2;
    }
}

这是使用视图的"活动"。。

public class MyActivity extends Activity {
    // This is only here because findViewById() returns NULL when I search
    private MyView myView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Resources res = getResources();
        myView = new myView(this);
        setContentView(myView);
        // This returns NULL.  :/       
        // final TextView textView1 = (TextView) findViewById(R.id.textView1);
        // This is only here because findViewById() returns NULL when I search..
        final TextView textView1 = myView.getTextView1();
        final Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_animation);
        textView1.setAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation _anim) {
                Log.v("InsideAnimation", "Animation ended");
                textView1.setVisibility(View.INVISIBLE);
            }
            @Override
            public void onAnimationRepeat(Animation _anim) {
                Log.v("InsideAnimation", "Animation repeated");
            }
            @Override
            public void onAnimationStart(Animation _anim) {
                Log.v("InsideAnimation", "Animation started");
            }
        });
        anim.reset();
        textView1.startAnimation(anim);
    }
}

主要的问题是,我加载的动画(在xml中定义)和启动完全没有任何作用。我得到了一个"动画开始了"的Log语句,但仅此而已。当我模仿它时,它永远不会结束,也不会做任何事情。就像它开始了,但实际上并没有运行。我认为问题是,动画可能希望使用xml id布局定义来完成它的工作,但由于findViewById()返回NULL,我假设该部分已损坏(因此动画无法工作)。所以至少现在,我想找出我在设置ID时做错了什么,这样我就可以让findViewById()返回正确的视图,而不是NULL。。。然后动画可能会起作用。(或者,如果在那之后它仍然不起作用,我将不得不考虑在代码中而不是在xml中定义动画)

如有任何帮助或建议,我们将不胜感激。

有一个setID方法用于对视图进行java编码。

我的猜测是,您的TextView目前还没有实例化。尝试在oncreate方法之外触发动画。

此外,您的MyView是从View扩展而来的,您正在内部创建RelativeLayout。如果你这样做,你也可以直接从RelativeLayout扩展你的类,然后做一些类似的事情

mRelativeLayout = new MyRelativeLayout(context)
mRelativeLayout.findViewById(R.id.textview1)

也可能是个问题,因为你的活动中没有提到你的亲属布局。尽管我的第一个猜测更有可能是解决方案。。。

哦,你可以简单地用xml设计你的RelativeLayout。我认为在这里使用编程布局没有什么实际意义。。。

最新更新