[原创]android framelayout&n…

来源:互联网 发布:知乎必读书单 编辑:程序博客网 时间:2024/06/06 02:31
这个类是我实际做android时候做activity内的浮动层效果时封装的类
如果一个界面有多个浮动层可以简单复制这个类快速做出多个浮动层来
 

public class PlayerHeader extends FrameLayout {
private static final String LOG_TAG =MainActivity.class.getName();
 

public PlayerHeader(Context context) {
super(context);
mContext = context;
}

public PlayerHeader(Context context, AttributeSet attrs){
super(context, attrs);
headerRoot = null;
mContext = context;
}

public PlayerHeader(Context context, AttributeSet attrs, intdefStyle) {
super(context, attrs, defStyle);
}
 
// 这里只调用一次,使用这个函数创建一个布局到framelayout
public void init(ViewGroup anchorView, VideoView videoview){
 //添加浮动层的framelayout
mAnchor = anchorView;
// init header -----------------
LayoutInflater inflate = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//新建的层放在framelayout顶部
FrameLayout.LayoutParams tlp1 = newFrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP);

// init header
headerRoot = inflate.inflate(R.layout.media_controler_header,null);
//这个只是示范代码,在这里记录所有控件的句柄
bTitle = (TextView)headerRoot.findViewById(R.id.video_title);
bIcon = (ImageView)headerRoot.findViewById(R.id.channel_icon);
mAnchor.addView(headerRoot, tlp1);

hide();

}
 
// 控制板显示/
public void show() {

headerRoot.setVisibility(VISIBLE);
mShowing = true;
}

public void hide() {
headerRoot.setVisibility(INVISIBLE);
mShowing = false;
}
}

使用方法
playerHeader= new PlayerHeader (this);
//找到要添加浮动层的framelayout
playerHeader.init((FrameLayout) findViewById(R.id.ff),player);
playerHeader.show()//显示
playerHeader.hide()//显示

原创粉丝点击