简单浮层窗口

本文介绍如何在Android应用中创建并使用浮层布局。通过定义一个包含按钮和图片的简单XML布局文件,并在主活动中通过代码将其作为浮层添加到主布局上。文章还展示了如何为浮层中的元素添加点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先创建一个要浮层的布局fuceng.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textColor="#fff"
        android:text="点这里" />

    <ImageView
        android:id="@+id/haha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/haha"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:text="我知道了" />

</RelativeLayout>

这里写图片描述

然后需要给主类的布局设置一个ID,找到这个布局,然后通过它找到父控件FrameLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rel"
    >

在这个FrameLayout中添加View,也可以实现监听事件,进行跳转,实现简单的浮层

可以添加ProgressBar,没有白色背景

        //find主布局
        RelativeLayout rel = (RelativeLayout) findViewById(R.id.rel);
        //获取FrameLayout,应该是总的布局,帧布局
        parent = (FrameLayout) rel.getParent();
        //创建一个view
        view = View.inflate(MainActivity.this, R.layout.fuceng, null);
        //这里可以为view中的控件添加监听事件
        //设置背景为透明色
        view.setBackgroundColor(Color.parseColor("#88000000"));
        //添加到这个FrameLayout中
        parent.addView(view);

具体示例:

public class MainActivity extends Activity {

    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rel = (RelativeLayout) findViewById(R.id.rel);
        parent = (FrameLayout) rel.getParent();
        view = View.inflate(MainActivity.this, R.layout.fuceng, null);
        ImageView haha = (ImageView) view.findViewById(R.id.haha);
        Button back = (Button) view.findViewById(R.id.back);
        haha.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                startActivity(intent); 

            }
        });
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                parent.removeView(view);

            }
        });
        view.setBackgroundColor(Color.parseColor("#88000000"));
        parent.addView(view);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值