Android - ViewSwitcher 不切换视图

发布于 2024-09-24 12:26:53 字数 1121 浏览 4 评论 0原文

这应该很容易,对吧?所以 - 这是我如何在 XML 中定义 ViewSwitcher (为简洁起见,省略了 Id 和布局)

<ViewSwitcher android:layout_height="wrap_content" android:layout_width="fill_parent" >       
    <!-- First view, comes up OK --> 
    <LinearLayout android:id="@+id/header1">
        <!-- some more controls: Progress bar, test view and the button -->
    </LinearLayout>
    <!-- second view. changing to the actual (not include) layout has no effect -->
    <include android:id="@+id/header2" />
</ViewSwitcher>

然后在我的 Java 代码中的某个地方我有这段代码

ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
// more code that basically executes background search
// when call comes back - switch
switcher.bringToFront(); // does nothing
// switcher.bringChildToFront(findViewById(R.id.header2)); // no effect ether

它只是没有切换。我针对 API v. 3 (1.5+) 进行开发,令我惊讶的是,很少有对 ViewSwitcher 的引用。我在这里遗漏了一些明显的东西吗?

PS 我刚刚通过暴力发现这有效:

switcher.setDisplayedChild(1);

仍然 - 为什么 bringToFront() 没有运气?

It should be easy, right? So - here's how I have ViewSwitcher defined in XML (Id's and layouts omitted for brevity)

<ViewSwitcher android:layout_height="wrap_content" android:layout_width="fill_parent" >       
    <!-- First view, comes up OK --> 
    <LinearLayout android:id="@+id/header1">
        <!-- some more controls: Progress bar, test view and the button -->
    </LinearLayout>
    <!-- second view. changing to the actual (not include) layout has no effect -->
    <include android:id="@+id/header2" />
</ViewSwitcher>

Then somewhere in my Java code I have this code

ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
// more code that basically executes background search
// when call comes back - switch
switcher.bringToFront(); // does nothing
// switcher.bringChildToFront(findViewById(R.id.header2)); // no effect ether

It's just not switching. I develop for API v. 3 (1.5+) and to my surprise there are very few references to ViewSwitcher. I'm I missing something obvious here?

P.S. I just found out by brute force that this works:

switcher.setDisplayedChild(1);

Still - why no luck with bringToFront()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-10-01 12:26:53

ViewSwitcher 中,要在视图之间导航,您可以使用方法 showPrevious()showNext()

或者使用也可以通过 转到特定视图>setDisplayedChild(int index) 方法,其中索引可以是 01

In ViewSwitcher to navigate between views you can use the methods showPrevious() and showNext()

Or use also can go to specific view by setDisplayedChild(int index) method where index can be either 0 or 1

懒猫 2024-10-01 12:26:53

bringToFront() 实际上与 ViewSwitcher 无关,该方法继承自 View 类,代表当前视图的 z 顺序操作:https://developer.android.com/reference/android/view/View.html#bringToFront ()

您必须使用从 ViewAnimator 继承的 showNext()showPrevious() 方法。

bringToFront() actually has nothing to do with ViewSwitcher, the method is inherited from View class and stands for z-order manipulation of current view: https://developer.android.com/reference/android/view/View.html#bringToFront()

You have to use showNext() and showPrevious() methods inherited from ViewAnimator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文