Android中setLayoutParams要用父控件的LayoutParams
這幾天做程序想明白了一個問題,在此做一下記錄。
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顯示就會不正確。
一般情況這個問題是不會暴露的,只有你用的很細節的時候,才會出現。
總結
以上是生活随笔為你收集整理的Android中setLayoutParams要用父控件的LayoutParams的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最简单也最难——如何获取到Android
- 下一篇: Android中启动Activity(s