我想设置一个带有图案的动态按钮,可以随程序运行的状态和图片上方的文本变化而改变。如果我用ImageButton,连文本都无法添加。如果用Button,我可以添加文本,但是只能用android:drawableBottom设定图片和一些XML的相关属性。然而这些属性只能将文本和图片在二维空间里结合在一起,也就是说,我只能在文本周围添加图片,但不能将图片置于文本下方。
我应该怎么解决这个问题呢?
原问题:Android: combining text & image on a Button or ImageButton
黄舟2017-04-17 11:27:56
Answer: Vitaly Polonetsky
(Best answer)
You can set setBackgroundDrawable() on Button to add a background to the button. Any text can be placed above the background. If you need some xml related content, you can use the android:background attribute.
Answer: OneWorld
The following method mainly solves the operation of adding background, icon and text on a button:
<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>
If you want to achieve more complex effects, you can use RelativeLayout to set its clickability.
Answer: schlingel
I think there are some better solutions, just take a normal Button and do it with drawableLeft and gravity properties:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/my_btn_icon"
android:gravity="left|center_vertical" />
In this way, a button button will appear on the left side of the button, and the article will be on the right side of the button, centered.
Answer: CMA
Just use
android:background="@android:color/transparent"
android:drawableTop="@drawable/[your background image here]"
Alternative
android:background="@drawable/icon"
That’s it