如何在AppCompatButton的中心设置带有文本的可绘制图标

时间:2016-11-01 09:13:41

标签: android android-layout android-fragments android-appcompat

作为标题,我想在中心创建一个带有图标和文字的AppCompatButton。我使用了这种方法,但它不起作用:

Spannable buttonLabel = new SpannableString(" Button Text");
buttonLabel.setSpan(new ImageSpan(getApplicationContext(), R.drawable.icon,      
ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(buttonLabel);

我还在setTextAllCap布局中禁用了xml,但没有任何变化。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

使用填充

 <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/ic_refresh"
            android:paddingStart="@dimen/buttonPadding"
            android:paddingEnd="@dimen/buttonPadding"
            android:text="test" />

但是您需要处理多个屏幕尺寸