Android - ViewSwitcher 不切换视图
这应该很容易,对吧?所以 - 这是我如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
ViewSwitcher
中,要在视图之间导航,您可以使用方法showPrevious()
和showNext()
或者使用也可以通过
转到特定视图>setDisplayedChild(int index)
方法,其中索引可以是0
或1
In
ViewSwitcher
to navigate between views you can use the methodsshowPrevious()
andshowNext()
Or use also can go to specific view by
setDisplayedChild(int index)
method where index can be either0
or1
bringToFront()
实际上与ViewSwitcher
无关,该方法继承自 View 类,代表当前视图的 z 顺序操作:https://developer.android.com/reference/android/view/View.html#bringToFront ()您必须使用从
ViewAnimator
继承的showNext()
和showPrevious()
方法。bringToFront()
actually has nothing to do withViewSwitcher
, 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()
andshowPrevious()
methods inherited fromViewAnimator
.