最佳展示方式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背景。
对不起,我的英语不好。
快乐编码:)