java - 更改 TextSize 后 TextView 不会调整大小

标签 java android textview

我有一个 ConstraintLayout,里面有一个 TextViewEditTextTextView 位于左侧,当 EditText 获得焦点时,我希望 TextView 更改其 TextSize 并向右移动。当它失去焦点时,我想扭转它。

这是布局:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/custom_edit_text_constraint_layout"
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>

    <EditText
            android:id="@+id/custom_edit_text_text_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:paddingLeft="@dimen/custom_edittext_def_hint_margin"
            android:paddingStart="@dimen/custom_edittext_def_hint_margin"
            tools:ignore="RtlSymmetry"
    />

    <TextView
            android:id="@+id/custom_edit_text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginLeft="@dimen/custom_edittext_def_hint_margin"
            android:layout_marginStart="@dimen/custom_edittext_def_hint_margin"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是代码(如果我忘记了任何重要的部分,我可以编辑它):

hint = findViewById(R.id.custom_edit_text_hint);    //TextView
textField = findViewById(R.id.custom_edit_text_text_field);    //EditText
    textField.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                focusHint();
            } else {
                unfocusHint();
            }
        }
    });

private void focusHint() {
    hint.setTextSize(hintSizeFocused);
    hint.setTextColor(hintColorFocused);
    moveHintToRight();
}

private void unfocusHint() {
    hint.setTextColor(hintColor);
    if(textField.getText().toString().isEmpty()) {
        hint.setTextSize(hintSize);
        moveHintToLeft();
    }
}

private void moveHintToRight() {
    int horizontalDistance = textField.getWidth() - hint.getRight() - dpToPx(HINT_MARGIN_SIDE);
    TranslateAnimation anim = new TranslateAnimation(0, horizontalDistance, 0, 0);
    anim.setDuration(ANIMATION_DURATION);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            ConstraintLayout l = findViewById(R.id.custom_edit_text_constraint_layout);
            ConstraintSet set = new ConstraintSet();
            set.clone(l);
            set.clear(R.id.custom_edit_text_hint, ConstraintSet.LEFT);
            set.connect(R.id.custom_edit_text_hint, ConstraintSet.RIGHT, R.id.custom_edit_text_constraint_layout, ConstraintSet.RIGHT, dpToPx(HINT_MARGIN_SIDE));
            set.applyTo(l);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    hint.startAnimation(anim);
}

private void moveHintToLeft() {
    int horizontalDistance = - hint.getLeft() + dpToPx(HINT_MARGIN_SIDE);
    TranslateAnimation anim = new TranslateAnimation(0, horizontalDistance, 0, 0);
    anim.setDuration(ANIMATION_DURATION);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            ConstraintSet set = new ConstraintSet();
            ConstraintLayout l = findViewById(R.id.custom_edit_text_constraint_layout);
            set.clone(l);
            set.clear(R.id.custom_edit_text_hint, ConstraintSet.RIGHT);
            set.connect(R.id.custom_edit_text_hint, ConstraintSet.LEFT, R.id.custom_edit_text_constraint_layout, ConstraintSet.LEFT, dpToPx(HINT_MARGIN_SIDE));
            set.applyTo(l);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    hint.startAnimation(anim);
}

这很好用,但只有当我不调整 TextView 的 TextSize 时才有效。当我调整 TextSize 的大小时(如代码所示),hint.getLeft()hint.getRight() 返回值,TextView 会使用旧的 TextSize,这会导致 TextView 移动得太远或不够远。但这对我来说没有意义,因为我在开始动画之前调整了 TextSize 的大小,并且 TextView 的宽度设置为 wrap_content。有谁知道为什么这不起作用以及我该如何解决?

编辑:

为了进一步解释和简化问题到底是什么,我举了一个例子:

textView.setTextSize(12);
int width1 = hint.getWidth();
textView.setTextSize(18);
int width2 = hint.getWidth();

由于 TextView 的宽度设置为 wrap_content,当我更改 textSize 时宽度应该会改变(至少我是这么认为的)。但是 width1 和 width2 是一样的。我该如何解决?

编辑 2:

我解决了这个问题 this answer埃里克。

最佳答案

按如下方式添加 textview 的右侧和结束约束。如果您希望它位于中间,则设置 horizo​​ntal_bias = 0.5,或者如果您希望它位于左侧,则设置为 0.0,最后设置为右侧 1.0

<TextView
            android:id="@+id/custom_edit_text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginLeft="@dimen/custom_edittext_def_hint_margin"
            android:layout_marginStart="@dimen/custom_edittext_def_hint_margin"
            android:layout_constraintRight_toRightOf = "parent"
            android:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintHorizontal_bias="0.0"
          />

希望这会奏效。试试吧

关于java - 更改 TextSize 后 TextView 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57972091/

相关文章:

java - PowerMockito VerifyStatic 在 2.0.0-beta5 中不起作用

java - 如何在 Java 中的对象参数中初始化数组?

java - 实现ArrayList类Remove方法Java

java - 如何确定经度和纬度坐标以在用户位置周围创建 1 英里半径?

java - Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接

java - Sonar ANT 任务集成错误

java - 有很多 getters & setters 是个好主意吗?

android - 设置textView的边框和背景颜色

Android TextView 可绘制,更改可绘制和文本之间的填充?

android - 如何从 AsyncTask Activity 中设置 TextView 的文本?