有没有办法在一个活动中用不同的id多次调用setContentView(id)来呈现不同的视图,或者我一定要启动一个新的活动?
发布于 2011-02-09 05:44:22
根据Austyn的评论,我确实在另一篇文章中找到了一些关于如何使用ViewFlipper来实现这一点的指导(参见复选标记的顶部答案here)。
如果你不想使用ViewFlipper,我找到了一个很好的例子来说明如何在同一个here视图中的布局之间切换:
XML:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>代码:
private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}
private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}发布于 2010-10-26 05:00:21
不,你不能轻易地多次调用它。您要么需要完全删除所有视图,然后膨胀新布局,要么使用ViewFlipper (或FrameLayout)在不同视图之间切换。
顺便说一句,这个问题以前也被问过,尽管我不能立即找到它。
发布于 2016-03-01 23:10:02
您可以从您的活动中尝试以下操作:
getWindow().addContentView(View, ViewGroup.LayoutParams);两个内容视图将是一个在另一个之上。但是,没有直接的方法来删除以这种方式添加的特定视图。
还要注意,在最后一次调用之后调用setContentView将删除之前添加的所有内容视图。
https://stackoverflow.com/questions/4018772
复制相似问题