【问题标题】:Can I change the layouts randomly with ViewFlipper?我可以使用 ViewFlipper 随机更改布局吗?
【发布时间】:2012-07-01 01:24:41
【问题描述】:

我的问题是我想通过按Button 从布局 1 转到任何其他布局。 例如:

从带有 setDisplayChild(R.id.layout3) 的布局 1 到总共 4 个布局的布局 3(不起作用)。

我制作了一个包含所有四个布局的 container.xml,但是如果不使用 flipper.showNext()flipper.showPrevious() 两次或类似的方法,我不能直接从布局 2 转到布局 4,方法 @987654325 @ 和 showPrevious() 只是前进或后退,我需要转到任何其他布局。

ViewFlipper 是否有一种方法可以从布局 1(或其他布局)转到其他布局(不是相邻的布局)或 ViewFlipper 知道只是去下一个、上一个或第一个布局(子)?

【问题讨论】:

    标签: android android-layout viewflipper


    【解决方案1】:

    viewFlipper 上是否有方法可以从 layout1(或其他布局) 到其他布局(不是邻居布局)或脚蹼 知道下一步,上一个还是第一个布局(子)?

    是的,ViewAnimator 类中的 setDisplayedChild 方法是 ViewFlipper 的父类,它允许您将 ViewFlipper 移动到您想要的任何孩子。

    例如:从 layout1 和 setDisplayChild(R.id.layout3) 到 总共 4 个布局中的布局 3。 (不工作)

    setDisplayedChild 要求您将所需 ViewViewFlipper 元素中的 位置 传递给它,而不是像您这样的布局的 id用它。因此,如果您想跳转到ViewFlipper 的其中一个孩子,只需使用:

    viewFlipper.setDisplayedChild(2); // jumps to the third child
    viewFlipper.setDisplayedChild(2); // another call like the one below will not move the ViewFlipper as it is already at the third child
    viewFlipper.setDisplayedChild(1); // jumps to the second child
    

    如果您想让ViewFlipper 在其子代中随机移动,那么您可以:

    Random mRandom = new Random();
    viewFlipper.setDisplayedChild(mRandom.nextInt(4)); // 4 for 4 children
    

    【讨论】:

    • 非常感谢,我等了一会儿才收到回复,但我得到的解决方案非常棒!非常非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2010-09-19
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多