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

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

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

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


当前回答

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

我的建议是用你的风格制作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" />

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

其他回答

这段代码非常适合我

<LinearLayout
    android:id="@+id/choosePhotosView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:clickable="true"
    android:background="@drawable/transparent_button_bg_rev_selector">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/choose_photo"/>

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="@string/choose_photos_tv"/>

</LinearLayout>

你可以在按钮上调用setBackground()来设置按钮的背景。

任何文本都将出现在背景上方。

如果你正在寻找类似的xml有: Android:background属性,工作方式相同。

 <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"         
        />   

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

制作一个假按钮。

这是唯一的办法

  <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>