您的位置:首页 > 移动开发 > Android开发

安卓Android打造属于你自己的TitleBar自定义控件模版

2017-09-07 20:19 393 查看
在我们实现需求的时候,有时会遇到菜单栏的多次使用,内容不一,但是结构类似,所以我们就需要制作一个模版,以适配不同情况。大概的样式就是这样的:





下面我们就使用自定义控件实现一下这种需求。

1.先创建一个自定义控件,构造函数使用前三个就行

public TitleBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public TitleBar(Context context) {
this(context, null);
}

public TitleBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
this.mContext = context;

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TitleViewBar, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.TitleViewBar_model:
model = a.getInt(attr, 0);//拿到xml注册的属性值
modelInt = model;
break;
}
}
a.recycle();//必须有这句


为能了能在页面静态设置自定义控件的自定义属性,所以使用了TypedArray 来获取xml里面的自定义的属性 

解释一下:

attrs为检索的属性容器(集合好听些)

obtainStyledAttributes(attrs, R.styleable.TitleViewBar, defStyle, 0) :返回一个TitleViewBar控件的属性集合

a.recycle() :返回先前检索的数组,稍后再用,这语句必须要写上,就相当于show()方法一样

switch (attr) {
case R.styleable.TitleViewBar_model:
model = a.getInt(attr, 0);//拿到xml注册的属性值
modelInt = model;
break;
}
R.styleable.TitleViewBar_model




这是attrs资源文件设置的属性名称

并且定义了其属性为integer,我们获取其值以进行判断,给控件设置data数据等等

接下来是将我们写好的布局文件放入到控件中已备使用:

View view = LayoutInflater.from(context).inflate(R.layout.title_layout, null);
tv_center = (TextView) view.findViewById(R.id.tv_center);
img_back = (ImageView) view.findViewById(R.id.img_back);
tv_loction = (TextView) view.findViewById(R.id.tv_loction);
ft_right = (FrameLayout) view.findViewById(R.id.ft_right);

RelativeLayout relativeLayout = new RelativeLayout(context);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
DensityUtils.dp2px(mContext, 48));//dp转化为px
relativeLayout.setLayoutParams(layoutParams);

ft_right.setVisibility(VISIBLE);
ft_right.setOnClickListener(this);
img_back.setOnClickListener(this);

relativeLayout.addView(view);
addView(relativeLayout);

view_pop = LayoutInflater.from(mContext).inflate(R.layout.layout_titlebar_lv, null);
mListView = (ListView) view_pop.findViewById(R.id.lv_pop);
上面创建了两个布局文件 一个加入自定义控件(R.layout.title_layout),另一个加入到popupwindow中(R.layout.layout_titlebar_lv,),用以点击右上角图标弹出来

接下来别忘了设置数据setModel(model)

然后是popup和右上角图片的点击事件:

public void onClick(View view) {
if (view.getId() == R.id.ft_right) {
adapter = new ListViewAdapter(mContext,mData);
mListView.setAdapter(adapter);
popupWindow = new PopupWindow(view_pop, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
popupWindow.setFocusable(true);
// 点列表外关闭列表
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(tv_loction, DensityUtils.dp2px(mContext, -120), 0);
} else {

}
}
然后是listviw的item的点击事件:

public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
mListView.setOnItemClickListener(onItemClickListener);
}
然后设置好数据源:

/**
* 数据1
* @return
*/
private List<HashMap<String, Object>> getData1() {
List<HashMap<String, Object>> mData = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
map.put("icon", R.drawable.apply_angin);
map.put("text", "再次申请");
mData.add(map);
map = new HashMap<>();
map.put("icon", R.drawable.apply_spend);
map.put("text", "结算详情");
mData.add(map);
return mData;
}

/**
* 数据2
* @return
*/
private List<HashMap<String, Object>> getData2() {
List<HashMap<String, Object>> mData = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
map.put("icon", R.drawable.apply_time);
map.put("text", "本日费用");
mData.add(map);
map = new HashMap<>();
map.put("icon", R.drawable.apply_time);
map.put("text", "本月费用");
mData.add(map);
map = new HashMap<>();
map.put("icon", R.drawable.apply_time);
map.put("text", "本年费用");
mData.add(map);
return mData;
}
这样titlebar创建好了,然后是如何使用:

既然控件都创建好了,那么使用起来应该是很简单方便的,如果使用很复杂,那么只能说明你的自定义控件还不达标。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title_bar);

titleBar = (TitleBar) this.findViewById(R.id.titleBar);
button = (Button) this.findViewById(R.id.btn);
button.setOnClickListener(this);

titleBar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
showToast("000000");
break;
case 1:
showToast("1111");
break;
case 2:
showToast("22222");
break;
}
titleBar.dismissPopWindow();

}
});
}
相信大家都可以看到使用起来只需要调用刚刚我们创建好的接口,然后实现item的点击接口就行了

可能说的不是很明白,大家可以看下代码就知道了。

链接:http://download.csdn.net/download/qq_37173653/9968999

ps:下载csdn资源怎么设置不需要积分呀,大佬说一下!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐