Android: 如何根据另一个文本视图的内容以编程方式设置文本视图的文本大小

huangapple go评论54阅读模式
英文:

Android: How to set a textview's text size based on another text view programmatically

问题

我尝试将第二个文本视图的文本大小设置为基于第一个文本视图的文本大小。我尝试过:

binding.tvMovieDescriptionList.textSize = binding.tvMovieRating.textSize

但没有成功,有什么帮助吗?

英文:

So I have two text views, one a that has a specific height and auto-size properties implemented in it. The other is a normal multiple lined text view with wrap content as height.

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tvMovieRating"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tvOthersActivityMovieHeight"
            android:layout_marginStart="@dimen/text_view_movie_description_margin_start"
            android:layout_marginHorizontal="@dimen/mainCLMarginActivityMovie"
            android:text="@string/movieRatingHint"
            android:textColor="@color/white"
            app:autoSizeMaxTextSize="@dimen/all_auto_max_text_sizes"
            app:autoSizeMinTextSize="@dimen/all_auto_min_text_sizes"
            app:autoSizeStepGranularity="@dimen/all_auto_size_step_granularities"
            app:autoSizeTextType="uniform"
            app:layout_constraintTop_toBottomOf="@id/tvMovieNameAndDate"
            />

        <TextView
            android:id="@+id/tvMovieDescriptionList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/text_view_movie_description_margin_start"
            android:layout_marginHorizontal="@dimen/mainCLMarginActivityMovie"
            android:textSize="268sp"
            android:text="@string/lorem_ispum"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvNonChangeableDescriptionText"
            />

Now what I'm trying to set the 2nd textView's textSize size based on the first textView's textSize.
I've tried:

binding.tvMovieDescriptionList.textSize = binding.tvMovieRating.textSize

but it didn't work, any help?

答案1

得分: 2

你不能直接执行以下操作:setTextSize(getTextSize())

原因是getTextSize()返回的是精确像素大小,而setTextSize()设置的是缩放像素单位。这些函数的单位不匹配。

因此,最简单的方法是,在设置大小时提到单位:

binding.tvMovieDescriptionList.setTextSize(TypedValue.COMPLEX_UNIT_PX, binding.tvMovieRating.textSize)

或者你可以在设置之前自己计算缩放像素单位:

val metrics = resources.displayMetrics
binding.tvMovieDescriptionList.textSize = binding.tvMovieRating.textSize / metrics.density
英文:

You cannot directly do the following: setTextSize(getTextSize()).

The reason is the getTextSize() is returning the exact pixel size, meanwhile setTextSize() sets the scaled pixel unit. The unit does not align with these functions.

So for the simplest way, you can mention the unit when you are setting the size:

binding.tvMovieDescriptionList.setTextSize(TypedValue.COMPLEX_UNIT_PX, binding.tvMovieRating.textSize)

Or you can calculate the scaled pixel unit yourself before setting:

val metrics = resources.displayMetrics
binding.tvMovieDescriptionList.textSize = binding.tvMovieRating.textSize / metrics.density

答案2

得分: 0

也许你忘记通知你的绑定。绑定中的大多数更改需要通知它们以更新。我会尝试使用

binding.notify()

或者

binding.notifyAll()

另外,你也可以简单地将自动调整大小的尺寸复制到你的次要文本视图上。我不明白为什么你不能这样做,因为这只涉及两个文本视图,而不是1120个。

英文:

Perhaps you've forgotten to notify your binding. Most changes in bindings require you to notify them to update them. I would try

binding.notify()

or

binding.notifyAll()

On another note, you could always simply copy your auto-sizing dimensions over to your secondary text view. I don't see why you couldn't do that, as this is only two text views, not 1120.

huangapple
  • 本文由 发表于 2023年2月14日 05:06:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定