2011-09-28 102 views
0

現在,我努力完成一些任務,就像在自定義的LinearLayout中添加我的子ImageView之間的空白區域一樣簡單(修改後的RadioGroup設計用於實現可檢查的自定義ImageView ,沒有覆蓋在梅薩里)。長話短說,這些圖像是一個固定的尺寸(60x60dip),並且因爲它們是動態的(從網站),我必須動態地添加他們像這樣:Android - ImageView邊距不出現在LinearLayout

 for(int i = 0; i < num; i++){ 
      ImageViewRadioButton childImage = new ImageViewRadioButton(mContext); 
      float imagehWidthHeight = getResources().getDimension(R.dimen.image_width_and_height); 

      LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int) imageWidthHeight, (int) imageWidthHeight); 
      int imageSpacing = Utils.dipsToPixels(10, mContext); 
      int innerPadding = Utils.dipsToPixels(5, mContext); 

      imageParams.leftMargin = imageSpacing; 
      imageParams.rightMargin = imageSpacing; 
      childImage.setAdjustViewBounds(true); 
      childImage.setLayoutParams(imageParams); 
      childImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_pressed)); 
      childImage.setPadding(innerPadding, innerPadding, innerPadding, innerPadding); 
      childImage.setClickable(true); 
      //other non-image properties... 
      imageContainer.addView(childImage); 
     } 

,做工作的唯一的事情是填充,它將其正確地分隔開。但是,我沒有看到每個孩子的填充(邊距)之間有任何空間。我是否正確地做了這件事,還是有沒有更好的方法來做到這一點,而不是爲了考慮每個孩子的利潤率?

回答

0

您已經創建了imageParams,但是您沒有在代碼中使用該參數,而是使用swatchParams參數的imageParams。而且你還沒有輸入swatchParams參數的代碼。

+0

顯然,這是一個例子,所以這應該是imageParams。即使那時我創建了LayoutParams對象並將其傳遞到視圖中,它仍然無法工作。 –

相關問題