android – 多次调用setContentView()

前端之家收集整理的这篇文章主要介绍了android – 多次调用setContentView()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一种方法可以在一次活动中使用不同的id多次调用setContentView(id)来呈现不同的视图,还是绝对必须启动一个新的Activity?

解决方法

根据Austyn的评论,我确实设法找到一些关于如何使用ViewFlipper在另一篇文章中完成这一点的指导(参见顶部的答案 here.)

如果你不想使用ViewFlipper,我发现了一个很好的例子,说明如何在相同的视图here之间切换布局:

XML:

  1. <FrameLayout
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. xmlns:android="http://schemas.android.com/apk/res/android">
  5. <ImageView
  6. android:src="@drawable/icon"
  7. android:scaleType="fitCenter"
  8. android:layout_height="fill_parent"
  9. android:layout_width="fill_parent"/>
  10. <TextView
  11. android:text="Learn-Android.com"
  12. android:textSize="24sp"
  13. android:textColor="#000000"
  14. android:layout_height="fill_parent"
  15. android:layout_width="fill_parent"
  16. android:gravity="center"/>
  17. </FrameLayout>

码:

  1. private void SwitchLayout2() {
  2. RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
  3. RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
  4.  
  5. // Enable Layout 2 and Disable Layout 1
  6. Layout1 .setVisibility(View.GONE);
  7. Layout2.setVisibility(View.VISIBLE);
  8. }
  9.  
  10. private void SwitchLayout1() {
  11. RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
  12. RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
  13.  
  14. // Enable Layout 1 & Disable Layout2
  15. Layout1.setVisibility(View.VISIBLE);
  16. Layout2.setVisibility(View.GONE);
  17. }

猜你在找的Android相关文章