在自定义按钮的Textview中自动调整文本的实现

时间:2017-11-14 02:07:44

标签: android

我想添加textview的新功能,即自动调整文本。这意味着如果文本视图中有较长的文本,它将自动调整大小或调整字体以适合文本视图。

我在build.gradle中包含了appcompat-v7:26.0.0,以便为低端设备提供该功能。但我的问题是我想在我的自定义Button中实现它。我知道视图按钮中包含了一个文本视图。

如何在自定义按钮中启用该功能。

这是我的自定义按钮的代码:

public class GotoButton extends Button {
    public static final String TAG = GotoButton.class.getSimpleName();

    public interface FloaterDragListener {
        void onFloaterDragStart(float screenX, float screenY);
        void onFloaterDragMove(float screenX, float screenY);
        void onFloaterDragComplete(float screenX, float screenY);
    }

    int[] screenLocation = {0, 0};
    boolean inFloaterDrag;
    boolean inLongClicked;
    int untouchableSideWidth = Integer.MIN_VALUE;
    FloaterDragListener floaterDragListener;

    public GotoButton(final Context context) {
        super(context);
    }

    public GotoButton(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public GotoButton(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onConfigurationChanged(final Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        untouchableSideWidth = Integer.MIN_VALUE;
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        try {
            final int action = MotionEventCompat.getActionMasked(event);

            float x = event.getX();
            float y = event.getY();

            if (action == MotionEvent.ACTION_DOWN) {
                if (untouchableSideWidth == Integer.MIN_VALUE) {
                    untouchableSideWidth = getResources().getDimensionPixelSize(R.dimen.nav_prevnext_width) - getResources().getDimensionPixelSize(R.dimen.nav_goto_side_margin);
                }

                if (x >= 0 && x < untouchableSideWidth || x < getWidth() && x >= getWidth() - untouchableSideWidth) {
                    return false;
                }
            }

            getLocationOnScreen(screenLocation);
            float screenX = x + screenLocation[0];
            float screenY = y + screenLocation[1];

            if (action == MotionEvent.ACTION_DOWN) { // reset long-clicked status
                inLongClicked = false;
            }

            if (!inLongClicked) { // do not continue if finger is still down but it's because long click is in progress
                if (!inFloaterDrag) {
                    if (action == MotionEvent.ACTION_MOVE) {
                        if (x < 0 || y < 0 || x > getWidth() || y > getHeight()) {
                            cancelLongPress();
                            inFloaterDrag = true;
                            floaterDragListener.onFloaterDragStart(screenX, screenY);
                        }
                    }
                }

                // do not use "else"!
                if (inFloaterDrag) {
                    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                        inFloaterDrag = false;
                        floaterDragListener.onFloaterDragComplete(screenX, screenY);
                    } else {
                        floaterDragListener.onFloaterDragMove(screenX, screenY);
                    }
                }
            }
        }catch (Exception e){}
        return super.onTouchEvent(event);
    }

    public void setFloaterDragListener(final FloaterDragListener floaterDragListener) {
        this.floaterDragListener = floaterDragListener;
    }

    @Override
    public boolean performLongClick() {
        inLongClicked = true;
        return super.performLongClick();
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

正如Mike M.上面所说的,改为扩展AppCompatButton类,就像这样

public class GotoButton extends AppCompatButton { ... }

然后使用XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <GotoButton
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

</LinearLayout>

GotoButton的定义中,使用app:属性的auto...前缀来引用支持库非常重要。

如果您希望文本小于默认的12sp,请同时指定app:autoSizeMinTextSize属性。