答案 0 :(得分:2)
只需使用TextView。
<TextView
android:id="@+id/some_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
android:drawableRight="@drawable/arrow_right"
android:clickable="true"
android:focusable="true"
android:background="?android:selectableItemBackground"
/>
TextViews支持复合可绘制对象,您可以在其中指定要显示在文本旁边的可绘制对象。可以在上,下,右,左,开始,结束。
还请注意,我将TextView设置为可单击和可聚焦,并为它提供了Android的默认波纹背景(在按下时显示波纹效果)。
编辑
如果您需要箭头直接位于文本之后,则必须使用容器视图和子视图:
<LinearLayout
android:id="@+id/button_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:background="?android:selectableItemBackground">
<TextView
android:id="@+id/some_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/next" />
<ImageView
android:id="@+id/some_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/arrow_right" />
</LinearLayout>
您需要在LinearLayout上设置点击侦听器。
或者,将U + 276fa字符用作TextView文本的一部分:❯
。它不是>
,看起来更像箭头。
答案 1 :(得分:1)
尝试使用此自定义按钮,它将计算将图标直接放置在文本旁边的位置。
public class CenteredIconButton extends Button {
private static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;
private Rect textBounds = new Rect();
private Rect drawableBounds = new Rect();
public CenteredIconButton(Context context) {
this(context, null);
}
public CenteredIconButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.buttonStyle);
}
public CenteredIconButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (!changed) return;
final CharSequence text = getText();
if (!TextUtils.isEmpty(text)) {
TextPaint textPaint = getPaint();
textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
} else {
textBounds.setEmpty();
}
final int width = getWidth() - (getPaddingLeft() + getPaddingRight());
final int height = getHeight() - (getPaddingTop() + getPaddingBottom());
final Drawable[] drawables = getCompoundDrawables();
if (drawables[LEFT] != null) {
drawables[LEFT].copyBounds(drawableBounds);
int leftOffset =
(width - (textBounds.width() + drawableBounds.width()) + getRightPaddingOffset()) / 2 - getCompoundDrawablePadding();
drawableBounds.offset(leftOffset, 0);
drawables[LEFT].setBounds(drawableBounds);
}
if (drawables[RIGHT] != null) {
drawables[RIGHT].copyBounds(drawableBounds);
int rightOffset =
((textBounds.width() + drawableBounds.width()) - width + getLeftPaddingOffset()) / 2 + getCompoundDrawablePadding();
drawableBounds.offset(rightOffset, 0);
drawables[RIGHT].setBounds(drawableBounds);
}
if (drawables[TOP] != null) {
drawables[TOP].copyBounds(drawableBounds);
int topOffset =
(height - (textBounds.height() + drawableBounds.height()) + getBottomPaddingOffset()) / 2 - getCompoundDrawablePadding();
drawableBounds.offset(topOffset, 0);
drawables[TOP].setBounds(drawableBounds);
}
}
}