最佳展示方式Text
(button
带图片)

您的问题:如何text
显示imagebutton
?
答:你不能text
用显示imageButton
。在接受的答案中告诉的方法也不起作用。
因为
如果你使用android:drawableLeft="@drawable/buttonok"
那么你不能设置drawable
在center
.button
如果你使用android:background="@drawable/button_bg"
那么color
你的drawable
将被改变。
在 android 世界中,有数以千计的选项可以做到这一点。但是根据我的观点,我在这里提供了最好的替代方案。(见下文)
解决方案:cardView
使用LinearLayout
您drawable/image
使用 inLinearLayout
因为它显示在中心。在您的帮助下,textView
您可以text
对此进行设置。我们制作cardView
背景transparent
。
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_margin="16dp"
app:cardBackgroundColor="@android:color/transparent"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/your_selected_image"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Happy Coding"
android:textSize="33sp"
android:gravity="center"
>
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
这里我解释一些术语:
app:cardBackgroundColor="@android:color/transparent"
用于制作透明背景cardView
app:cardElevation="0dp"
用于隐藏周围的海拔线cardView
app:cardUseCompatPadding="true"
它提供的实际大小为cardView
. 使用时始终使用它cardView
将您设置image/drawable
为LinearLayout
背景。
对不起,我的英语不好。
快乐编码:)