【问题标题】:Android add text in a drawable buttonAndroid 在可绘制按钮中添加文本
【发布时间】:2016-01-19 17:58:30
【问题描述】:

如何在可绘制图像按钮中添加文本,而不使用命令“android:text="test" 在主 xml 文件中添加经典方式的文本,但我想在可绘制 xml 中添加图像按钮的文本(代码 2)。

代码 1 - main.xml:

     <ImageButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/start_off"
        android:text="TEST"
        android:background="@null"/>

代码 2 - 可绘制的 xml 按钮:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Define the background properties like color etc -->
<item android:id="@android:id/background">
    <shape
        android:shape="oval">

        <gradient
            android:angle="270"
            android:startColor="#990000"
            android:endColor="#ff0000"
            />

        <corners
            android:radius="4dp"
            />

        <size
            android:width="120dp"
            android:height="120dp"
            />

    </shape>
</item>

【问题讨论】:

  • 因为我使用这个drawbale代码通过USB更新按钮的状态(如果它是打开或否),我只能使用图像按钮。

标签: android xml imagebutton


【解决方案1】:

为什么不使用按钮并将drawable作为背景?这是一个很好的链接查看here

【讨论】: