我试图在一个按钮上有一个图像(作为背景),并动态添加,这取决于在运行时发生的事情,一些文字上方/上方的图像。

如果我使用ImageButton,我甚至没有添加文本的可能性。 如果我使用按钮,我可以添加文本,但只定义一个图像与android:drawableBottom和类似的XML属性定义在这里。

然而,这些属性只在x和y维度上组合文本和图像,这意味着我可以在文本周围绘制图像,但不能在文本下面/下面绘制图像(z轴定义为显示出来)。

有什么建议吗?一种想法是扩展Button或ImageButton并重写draw()方法。但以我目前的知识水平,我真的不知道如何做到这一点(2D渲染)。也许有更有经验的人知道一个解决方案,或者至少有一些开始的建议?


当前回答

 <Button android:id="@+id/imeageTextBtn" 
        android:layout_width="240dip"
        android:layout_height="wrap_content"
        android:text="Side Icon With Text Button"
        android:textSize="20sp"
        android:drawableLeft="@drawable/left_side_icon"         
        />   

其他回答

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/temp"
        />
<Button
     android:id="@+id/groups_button_bg"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="Groups"
     android:drawableTop="@drawable/[image]" />


android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop

http://www.mokasocial.com/2010/04/create-a-button-with-an-image-and-text-android/

只是替换

android:background="@drawable/icon"

with

android:background="@android:color/transparent"
android:drawableTop="@drawable/[your background image here]"

伊茨的把戏很不错。;)

我采取了一种不同于这里所述的方法,它真的很有效,所以我想分享它。

我正在使用样式创建一个自定义按钮,图像在左边,文本在中右。只需遵循以下4个“简单步骤”:

I.使用至少3个不同的PNG文件和工具创建9个补丁:/YOUR_OWN_PATH/android-sdk-mac_x86/tools/./draw9patch。在这之后,你应该:

Button_normal.9.png, button_focused.9.png和button_pressed.9.png

然后下载或创建一个24x24的PNG图标。

ic_your_icon.png

保存在你的Android项目的drawable/文件夹中。

2在项目的drawable/文件夹下创建一个名为button_selector.xml的XML文件。状态应该是这样的:

<item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_focused" />
<item android:drawable="@drawable/button_normal" />

3转到values/文件夹,打开或创建styles.xml文件,并创建以下XML代码:

<style name="ButtonNormalText" parent="@android:style/Widget.Button">
    <item name="android:textColor" >@color/black</item>
    <item name="android:textSize" >12dip</item>
    <item name="android:textStyle" >bold</item>
    <item name="android:height" >44dip</item>
    <item name="android:background" >@drawable/button_selector</item>
    <item name="android:focusable" >true</item>
    <item name="android:clickable" >true</item>
</style>

<style name="ButtonNormalTextWithIcon" parent="ButtonNormalText">
    <item name="android:drawableLeft" >@drawable/ic_your_icon</item>
</style>

ButtonNormalTextWithIcon是一个“子样式”,因为它扩展了ButtonNormalText(“父样式”)。

注意,将ButtonNormalTextWithIcon样式中的drawablleft更改为drawableRight, drawableTop或drawableBottom,您可以将图标放在相对于文本的其他位置。

IV.转到你有UI XML的布局/文件夹,然后转到你想应用样式的按钮,让它看起来像这样:

<Button android:id="@+id/buttonSubmit" 
android:text="@string/button_submit" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
style="@style/ButtonNormalTextWithIcon" ></Button>

和…瞧!你的按钮在左边有一个图像。

对我来说,这是更好的方法!因为这样做,你可以管理按钮的文本大小与你想要显示的图标分开,并使用相同的背景绘制几个按钮与不同的图标,尊重Android UI指南使用风格。

你也可以为你的应用程序创建一个主题,并添加“父样式”,这样所有的按钮看起来都是一样的,并只在你需要的地方应用“子样式”的图标。

MaterialButton支持设置图标并将其与文本对齐:

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My button"
        app:icon="@drawable/your_icon"
        app:iconGravity="textStart"
        />

iconGravity也可以是start / end,如果你想让图标与按钮对齐,而不是与按钮内的文本对齐。


从1.5.0-beta01版本开始,app:iconGravity也可以是top / textTop(提交)