Java android中带有图像、文本和颜色的按钮背景

Java android中带有图像、文本和颜色的按钮背景,java,android,xml,Java,Android,Xml,我想在android中创建一个带有背景色、图标和文本的按钮。 但我不知道如何添加图像。我的XML是: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_paren

我想在android中创建一个带有背景色、图标和文本的按钮。 但我不知道如何添加图像。我的XML是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="12dp"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            android:text="BOTON"
         />

    </TableLayout>
</LinearLayout>

我的背景是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item >
     <shape android:shape="rectangle"  >
         <corners android:radius="6dip" />
         <stroke android:width="1dip" android:color="#5e7974" />
         <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />  

     </shape>
 </item> 

</selector>

我想做一些像这样的:


如何在“我的背景”按钮中添加背景图像和文本?我试着用属性android:drawableleft放置图标,但对于按钮的大小来说,图标的大小太小了…

这其实并不难。我只是为了我自己的项目才这么做

Android的
按钮
s在XML中有一个
drawableLOCATION
属性

要在顶部放置图像,您可以使用

android:drawableTop="@drawable/your_drawable_icon"
所有四种可能的选择都是:

android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop
要调整图像图标的间距/填充,可以使用

android:drawablePadding="12dp"
你可以阅读更多关于这方面的内容

如果这对你来说不够准确,你可以用一个RelativeLayout,把东西放进去让它看起来正确

我必须为我目前的项目这样做。因此,我的XML中没有按钮,而是有以下内容:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/applianceInfoLayout"
    android:clickable="true"
    android:background="@drawable/button_background" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/service_menu_icon_padding"
        android:layout_marginRight="@dimen/service_menu_icon_padding"
        android:id="@+id/infoTextView"
        android:src="@drawable/ic_applianceinfo"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:layout_centerVertical="true"
        android:text="Info"
        android:textColor="#E6E7E8"
        android:fontFamily="sans-serif-light"
        android:textAllCaps="false"
        android:textSize="22sp"
        android:layout_toRightOf="@id/infoTextView"/>

</RelativeLayout>

这给了我这个:


我已经这样做了,但我有一个问题。当我的按钮增长时,图标背景不会增长,结果是:此:。“我该怎么做才能让图标更大?”@Josue您可能需要提供一个更大的可绘制图标。或者只使用RelativeLayout而不是按钮