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

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

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

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


当前回答

对于那些只是想把背景,图标图像和文本放在一个按钮从不同的文件:设置一个按钮背景,drawableTop/Bottom/ right /Left和填充属性。

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/home_btn_test"
        android:drawableTop="@drawable/home_icon_test"
        android:textColor="#FFFFFF"
        android:id="@+id/ButtonTest"
        android:paddingTop="32sp"
        android:drawablePadding="-15sp"
        android:text="this is text"></Button>

对于更复杂的布局,你也可以使用RelativeLayout(或任何其他布局),并使其可点击。

教程:涵盖这两种情况的优秀教程:http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx

其他回答

这段代码非常适合我

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

你可以使用drawableTop(也可以使用drawableLeft等)来绘制图像,并通过添加重力左|center_vertical来设置图像下面的文本

<Button
            android:id="@+id/btn_video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:drawableTop="@drawable/videos"
            android:gravity="left|center_vertical"
            android:onClick="onClickFragment"
            android:text="Videos"
            android:textColor="@color/white" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/temp"
        />

对于那些只是想把背景,图标图像和文本放在一个按钮从不同的文件:设置一个按钮背景,drawableTop/Bottom/ right /Left和填充属性。

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/home_btn_test"
        android:drawableTop="@drawable/home_icon_test"
        android:textColor="#FFFFFF"
        android:id="@+id/ButtonTest"
        android:paddingTop="32sp"
        android:drawablePadding="-15sp"
        android:text="this is text"></Button>

对于更复杂的布局,你也可以使用RelativeLayout(或任何其他布局),并使其可点击。

教程:涵盖这两种情况的优秀教程:http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx

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

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

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

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