微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有以下布局与imageview和textfield,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="26dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/a01" />

     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="31dp"
        />

</RelativeLayout>

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

解决方法

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

然后当你想删除它:

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

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

原文地址:https://www.jb51.cc/android/314066.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐