gpt4 book ai didi

Android: addview()- 在 Activity 之上添加新 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:56 25 4
gpt4 key购买 nike

我有以下带有 ImageView 和文本字段的布局,

<?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>

此布局将是透明的,我想在特定 Activity 之上调用此布局,当特定 Activity 首次启动时,如何使用 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));.

关于Android: addview()- 在 Activity 之上添加新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com