Android基础之FrameLayout的再认识——模拟PopupWindow弹出框效果

FrameLayout即帧布局,以往开发的时候用的比较少,但最近做到一些弹出框的效果,就不得不使用帧布局来为底层的View来遮盖,中层用透明的view,最后再在上层铺弹出的View来达到既定的效果。

分析一下如下的一张效果图:

        

        该View就是由 底层为粉色 覆盖上一层半透明view 然后再在上层添加一层白色背景的效果图。

以此是否可以模拟顶部弹出的弹出框呢。

public class CustomFrame extends LinearLayout{
    /**
     *  承载内容的主体view
     * */
    private FrameLayout mFrameContent;
    private Context mContext;
    //遮罩颜色
    private int maskColor = 0x88888888;
    public CustomFrame(Context context) {
        this(context,null);
    }

    public CustomFrame(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CustomFrame(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(context,attrs);
    }
    
    /**
     *   初始化View :
     *   这里通过java代码,添加入内,并没有使用什么xml布局
     * */
    private void init(Context context, AttributeSet attrs) {
        // 创建布局
        mFrameContent = new FrameLayout(context);
        // 设置Frame的信息包 为铺满全屏 new xxx.LayoutParams(x,y)
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        // 将信息包设置给view
        mFrameContent.setLayoutParams(params);
        // 将View添加给父布局
        addView(mFrameContent);
    }
    
    /**
     *  将底层 半透明层和上层添加入内
     * */
    public void setCustomFrameView(View beyondView,View topView){
        mFrameContent.addView(beyondView,0);
        
        View maskView = new View(mContext);
        maskView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        maskView.setBackgroundColor(maskColor);
        mFrameContent.addView(maskView,1);
        
        mFrameContent.addView(topView);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.zxl.myworklearning.activities.views.activities.FrameCusActivity">
    <com.example.zxl.myworklearning.widget.CustomFrame
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.example.zxl.myworklearning.widget.CustomFrame>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:background="#fff"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <TextView
            android:layout_marginTop="200dp"
            android:layout_marginLeft="100dp"
            android:text="FrameLayout透明度学习"
            a
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值