代码之家  ›  专栏  ›  技术社区  ›  jakub

为Android按钮添加箭头的最简单方法是什么

  •  0
  • jakub  · 技术社区  · 6 年前

    我需要在下一个按钮上添加箭头

    enter image description here

    最简单的方法是什么?使自定义复合视图听起来过于复杂

    PS符号 > 不是很好。

    2 回复  |  直到 6 年前
        1
  •  2
  •   TheWanderer    6 年前

    只需使用文本视图。

    <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>
    

    您需要将单击侦听器设置为线性布局。

    或者,使用U+276fa字符作为TextView文本的一部分: ❯ . 不是的 > 看起来更像一支箭。

        2
  •  1
  •   Silvia H    6 年前

    试试这个自定义按钮,它会计算将图标直接放在文本旁边的位置。

    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);
        }
    }
    

    }