在运行时更改字体大小android

sim*_*ple 3 android android-fonts android-xml

我有一个textview和它大小32spxml

android:textSize="32sp"
Run Code Online (Sandbox Code Playgroud)

我想以28sp编程方式更改它。所以我使用了下面的代码。

txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.twenty_eight_sp));
Run Code Online (Sandbox Code Playgroud)

但是上面的代码是添加28sp32sp. 所以字体变得太大了。我不想添加字体大小,我想设置一个新的字体大小。

任何人都可以建议我。

Ras*_*ngh 7

您必须将其更改为,TypedValue.COMPLEX_UNIT_PX因为从资源getDimension(id)返回一个dimen值并隐式转换为px.

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
           getResources().getDimension(R.dimen.result_font));
Run Code Online (Sandbox Code Playgroud)