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

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

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

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


当前回答

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(提交)

其他回答

也许我的解决方案适合很多用户,我希望如此。

我的建议是用你的风格制作TextView。它非常适合我,而且功能齐全,比如按钮。

首先让我们制作按钮样式,你可以在任何地方使用…我正在创建button_with_hover.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#8dbab3" />
            <gradient android:angle="-90" android:startColor="#48608F" android:endColor="#48608F"  />
        </shape>

        <!--#284682;-->
        <!--border-color: #223b6f;-->
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#284682" />
            <solid android:color="#284682"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="@color/ControlColors" />
            <gradient android:angle="-90" android:startColor="@color/ControlColors" android:endColor="@color/ControlColors" />
        </shape>
    </item>

</selector>

其次, 让我们创建一个textview按钮。

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dip"
    android:layout_gravity="right|bottom"
    android:gravity="center"
    android:padding="12dip"
    android:background="@drawable/button_with_hover"
    android:clickable="true"
    android:drawableLeft="@android:drawable/btn_star_big_off"
    android:textColor="#ffffffff"
    android:text="Golden Gate" />

这就是结果。然后用任何颜色或任何其他属性和边距设置自定义按钮的样式。祝你好运

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/temp"
        />

制作一个假按钮。

这是唯一的办法

  <FrameLayout
        android:id="@+id/fake_button"

        android:layout_width=" .. "
        android:layout_height=" .. "

        android:background="@android:color/transparent"

        android:clickable="true"
        android:onClick="tappedNext">

        <ImageView
            android:id="@+id/fake_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:src="@drawable/your_amazing_drawable" />

        <TextView
            android:id="@+id/fake_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"

            android:text="Next"
            android:fontFamily="@font/ .. "
            android:textColor="@color/ .. "
            android:textSize=" .. " />
    </FrameLayout>
<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/

这个问题有一个更好的解决方案。

只需要一个普通的按钮,使用drawablleft和重力属性。

<Button
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:drawableLeft="@drawable/my_btn_icon"
  android:gravity="left|center_vertical" />

通过这种方式,您可以得到一个按钮,它在按钮的左侧显示一个图标,而文本在图标的右侧垂直居中。