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