Java 更改TextSize后,TextView不会调整大小

Java 更改TextSize后,TextView不会调整大小,java,android,textview,Java,Android,Textview,我有一个ConstraintLayout,里面有TextView和EditText。TextView位于左侧,当EditText获得焦点时,我希望TextView更改其文本大小并移到右侧。当它失去焦点时,我想反转它 这是布局图: <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/custom_edit_text_constraint_layout" xmlns:tools="http://

我有一个
ConstraintLayout
,里面有
TextView
EditText
TextView
位于左侧,当
EditText
获得焦点时,我希望TextView更改其文本大小并移到右侧。当它失去焦点时,我想反转它

这是布局图:

<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>
这非常有效,但仅当我不调整
TextView
的TextSize时。当我调整文本大小时(如代码所示),
hint.getLeft()
hint.getRight()
返回值,
TextView
将使用旧的TextSize返回值,这会导致
TextView
移动太远或不太远。但这对我来说没有意义,因为我在开始动画之前调整了TextSize的大小,并且
TextView
的宽度设置为
wrap\u content
。有人知道为什么这不起作用,以及我如何修复它吗

编辑:

为了进一步解释和简化问题,我举了一个例子:

textView.setTextSize(12);
int width1 = hint.getWidth();
textView.setTextSize(18);
int width2 = hint.getWidth();
由于
TextView
的宽度设置为
wrap\u content
,因此当我更改textSize时,宽度应该会更改(至少我认为是这样)。但是宽度1和宽度2是相同的。我怎样才能解决这个问题

EDIT2:

我用计算机解决了这个问题
Eric所作。

按如下方式添加文本视图的右端约束。如果希望文本视图位于中间,则将水平偏移设置为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"
          />


希望这能起作用。尝试一下如下添加textview的右端和右端约束。如果希望它位于中间,则将水平偏移设置为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"
          />


希望这能奏效。试试吧

谢谢你的回答!但动画后的固定位置是正确的。问题在于
TextView
在动画期间移动的位置。它移动得太远/不够远,但当我设置新约束时,它位于正确的位置。所以,问题是
水平距离
您可以通过将水平偏差从0.0更改为1.0来移动它。我需要通过动画以编程方式移动它。好的。。给我一些时间,我会给你发演示。。如何以编程方式设置动画。编辑一个例子来更好地简化我的问题是很容易的。也许你现在可以帮我。谢谢你的回答!但动画后的固定位置是正确的。问题在于
TextView
在动画期间移动的位置。它移动得太远/不够远,但当我设置新约束时,它位于正确的位置。所以,问题是
水平距离
您可以通过将水平偏差从0.0更改为1.0来移动它。我需要通过动画以编程方式移动它。好的。。给我一些时间,我会给你发演示。。如何以编程方式设置动画。编辑一个例子来更好地简化我的问题是很容易的。也许你现在可以帮我。