这几天做程序想明白了一个问题,在此做一下记录。

ActionBarCustomView customview = new ActionBarCustomView(context);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
Gravity.CENTER_VERTICAL);
customview.setLayoutParams(lp);

给ActionBar上加子view,ActionBarCustomView是自己写好的子view。

其中父控件是ActionBar,那就必须写成ActionBar.LayoutParams

如果父控件是LinearLayout,那就必须写成LinearLayout.LayoutParams

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;

子view的布局是要以父控件为基础的,如果你的子布局是依靠父亲布局来显示的,就必须这么设置,要不子view在父view显示就会不正确。

一般情况这个问题是不会暴露的,只有你用的很细节的时候,才会出现。

 

相关文章:

  • 2021-11-08
  • 2022-12-23
  • 2021-10-18
  • 2021-09-13
  • 2021-12-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案