2012-09-19 147 views
9

我想添加一个图像到Button(没有imagebutton,因为特定的按钮可以包含文本或图像;加上图像按钮有相同的问题),同时仍然保留原始的Android风格按钮。因此,在XML中添加android:background并不会削减它,因为这将删除带有圆角等的Android默认按钮。(android.R.drawable.btn_default)Android将图像添加到按钮

这是以任何方式可能吗?

我想一个办法是让对按下按钮9patch图像(一个用于向上,一个用于向下)和onTouch Action_DOWN使分层背景绘制,并将该到按钮,然后做同样的事情,但与其他9patch对于onTouch Action_UP,但我认为这会大大降低应用程序的性能,因为对于所有按钮点击(对于我的应用程序来说,这将会相当多)需要大量的资源阅读和图层合并。我上面说的是正确的吗?

编辑:我不能在XML中声明图像的来源,因为我从Web服务获取图像,所以任何东西都可以放在按钮上,但它必须以编程方式发生。

+0

你可以设置你想要backgroundDrawable随时与setBackgroundDrawable(R.drawable.my_selector)http://stackoverflow.com/questions/12492064/android-add-image-to-button/ 12492097#12492097 – weakwire

+0

那么通过网络服务获得的图像呢? – stealthjong

回答

21

你可以在你layout.xml使用android:drawableTopandroid:drawableBottomandroid:drawableLeftandroid:drawableRight属性来完成。

+0

无法使用XML,请参阅编辑。 – stealthjong

+1

在代码中,您可以使用 - strangly命名方法'setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)',如下所述:http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds (int,int,int,int)< - 链接没有正确地链接到SO ... – Ridcully

+0

只读,你从web服务获取图像,所以你最好使用这个方法,然后:'setCompoundDrawablesWithIntrinsicBounds(Drawable left,Drawable top ,Drawable right,Drawable bottom)'。你可以使用Drawable来创建Drawable。createFromStream(InputStream is)'。 – Ridcully

0

您可以创建自己的绘制与多个国家,并设置背景

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" 
     android:drawable="@drawable/selected" /> 
    <item android:drawable="@drawable/normal" /> 
</selector> 

绘项目可以有圆角像这样。

<corners android:topLeftRadius="5dp" android:topRightRadius="5dp" 
     android:bottomLeftRadius="0.2dp" android:bottomRightRadius="0.2dp"/> 
8

以下设置按钮的属性显示图像与文本,使用此属性图像显示上面的文本。

<Button android:drawableTop="@drawable/ic_launcher"/> 
2

用于设定图像:

btn.setBackgroundResource(R.drawable.ic_launcher); 

有关设置文本:

btn.setText("My Button"); 

代码:

private Drawable buttonDrawable; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button btn=(Button) findViewById(R.id.button1); 
    buttonDrawable=btn.getBackground(); 
    //Setting the image. 
    btn.setBackgroundResource(R.drawable.ic_launcher); 
    btn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      /*Removing image and setting text*/ 
      btn.setBackgroundDrawable(buttonDrawable); 
      btn.setText("My Button"); 
     } 
    }); 
} 
+1

将不起作用,因为我想保留Android风格的按钮。当我设置背景时,android原始文件不存在,并替换为可能的方形按钮。我想在按钮上/中显示图像,而不是原始按钮布局 – stealthjong

8

enter image description here

 <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:background="@drawable/home_button" 
      android:drawableLeft="@android:drawable/ic_menu_edit" 
      android:drawablePadding="6dp" 
      android:gravity="left|center" 
      android:height="60dp" 
      android:padding="6dp" 
      android:text="AndroidDhina" 
      android:textColor="#000" 
      android:textStyle="bold" /> 

更多信息http://androiddhina.blogspot.in/2015/09/how-to-add-image-inside-button.html