2014-03-03 35 views
0

我已經看過這個各種問題一個動態按鈕的圖像,但是,不能讓他們的工作。如何設置頂部填充,用圖片和文字

我有一個按鈕,背景,圖像在上面,文字在底部。該按鈕是動態的,因此NO XML可用。

代碼動態按鈕:

  final Button image = new Button(this); 
      image.setTag(i); 
      image.setText(buttonsList[i].toUpperCase()); 
      image.setGravity(Gravity.CENTER); 
      image.setBackgroundColor(getResources().getColor(R.color.dark_grey)); 

      if (i == Const.MainIndex.FESTIVAL_OUTLOOK_INDEX) { 

       try { 
        XmlResourceParser parser = getResources().getXml(
          R.color.text_color_green); 
        ColorStateList colors = ColorStateList.createFromXml(
          getResources(), parser); 
        image.setTextColor(colors); 
       } catch (Exception e) { 

       } 

       //setting this value to -ve decreases the 
          //gap between the image and text 
       image.setCompoundDrawablePadding(-100); 
       image.setCompoundDrawablesWithIntrinsicBounds(
         null, 
         getResources().getDrawable(
           R.drawable.sidemenu_image_festout), null, null); 




       image.setBackgroundResource(R.drawable.button_background_green); 
      } 

這裏是像現在的樣子..

enter image description here

我想設置填充在圖像的頂部。以便圖像看起來不接近按鈕的邊界。我怎樣才能做到這一點?

我試過設置image.setPadding(0,10,0,0)但沒有效果。

+0

的圖像 –

+0

嘗試保證金也許你應該做一個轉換爲DP,這個問題可以幫助你:http://stackoverflow.com/questions/9685658 – Fllo

+0

@Fllo,我給它是一個嘗試。沒有效果。 –

回答

0
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
lp.setMargins(left, top, right, bottom); 
imageView.setLayoutParams(lp); 

使用本

+0

它是一個動態按鈕,我如何得到LayoutParams? –