Android:addview() – 在活动之上添加新视图

前端之家收集整理的这篇文章主要介绍了Android:addview() – 在活动之上添加新视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下布局与imageview和textfield,
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7.  
  8. <ImageView
  9. android:id="@+id/imageView1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentRight="true"
  13. android:layout_alignParentTop="true"
  14. android:layout_marginRight="26dp"
  15. android:layout_marginTop="22dp"
  16. android:src="@drawable/a01" />
  17.  
  18. <TextView
  19. android:id="@+id/textView1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentBottom="true"
  23. android:layout_alignParentLeft="true"
  24. android:layout_alignParentRight="true"
  25. android:layout_below="@+id/imageView1"
  26. android:layout_marginTop="31dp"
  27. />
  28.  
  29. </RelativeLayout>

这个布局是透明的,我想在特定活动的基础上调用此布局,当特定活动首次启动时,如何使用addview()实现它?

解决方法

当你想要展示它时:
  1. FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
  2. View.inflate(this,R.layout.overlay_layout,rootLayout);

然后当你想删除它:

  1. FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
  2. rootLayout.removeViewAt(rootLayout.getChildCount()-1);

这是一个简洁的解决方案,您应该通过在XML文件中为RelativeLayout提供一个id来删除View,然后删除:rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));.

猜你在找的Android相关文章