天天看点

android 自定义actionbar view,android中使用setCustomView不能填满actionbar的问题

在安卓原生日历中日程的编辑界面的title是用setCustomView来自定义的。自定义actionbar的代码基本是如下形式:ViewGroup customView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.pen_note_custom_title, null);

getActionBar().setCustomView(customView);

一直以来我都是这样做的,但是当我尝试达到如下效果的时候,却怎么也做不到。

android 自定义actionbar view,android中使用setCustomView不能填满actionbar的问题

取消和完成分别是两个LinearLayout做成的,从上图可以看出,这两个LinearLayout是平分了actionbar的。平分actionbar是通过如下代码来设置LinearLayout的style:

match_parent

0dp

1

true

horizontal

但是按照刚刚给出的代码怎么也做不出日历效果来,通过给我自定义的布局填充一个背景发现我的自定义布局根本没有填满actionbar。

原来问题出这里:ViewGroup customView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.pen_note_custom_title, null);

日历中使用了不同的参数,有三个参数:View actionBarButtons = inflater.inflate(R.layout.edit_event_custom_actionbar,

new LinearLayout(mContext), false);

其中第二个参数newLinearLayout(mContext)决定了该如何显示自定义的view。于是我改写了inflate的方式,问题解决了。