2013-10-11 67 views

回答

6

我遇到了同樣的問題,但與RelativeLayout。我通過將邊距放在ViewStub定義上來修復它。這可能不是最好的方法,最好的方法可能是使用層次佈局,其中根佈局的第一個孩子是設置了邊距幷包含所有其他元素的孩子。

不確定這是根本原因,但這些是我所知道的兩種解決方法。

2

方法1: 根據Android開發者網站,無論何時使用ViewStub,其佈局參數都會傳遞給膨脹的孩子。因此,要設置佈局參數,例如marginLeft,marginTop,marginRight,marginBottom,您必須將值設置爲ViewStub,它將被傳遞給充氣的孩子。

enter image description here

方法2:要不然ViewStub是可見後,您可以動態創建的LayoutParams並設置爲ViewGroup.This也適用。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     params.leftMargin =getResources().getDimensionPixelSize(R.dimen.four); 
     params.rightMargin=getResources().getDimensionPixelSize(R.dimen.four); 
     params.bottomMargin=getResources().getDimensionPixelSize(R.dimen.eight); 
     findViewById(R.id.linearLayout).setLayoutParams(params);