TextView 大小问题 android
TextView size issue android
我从 dimens.xml 设置了 texview 大小。但它在不同的移动屏幕上表现不同,例如,In honor 6x 设备看起来字体很大,而 intex 移动设备看起来很小但我应用相同的文本大小。
这是我的代码:-
private fun changeStandardDialog(standardList: ArrayList<Category>) {
val factory = LayoutInflater.from(this)
val standardDialog = factory.inflate(R.layout.select_standard_diolog, null)
selectedStandardId = SettingsHandler(this).getSettings("default_standard")
for (item in standardList) {
val rdbtn = RadioButton(this)
rdbtn.id = item.id
rdbtn.text = item.title
if (selectedStandardId.toInt() == item.id) {
rdbtn.isChecked = true
}
rdbtn.textSize = resources.getDimension(R.dimen.radio_text_size)
val textColor = Color.parseColor("#323642")
rdbtn.setButtonTintList(ColorStateList.valueOf(textColor));
rdbtn.setPadding(20, 30, 30, 30)
standardDialog.selectSubjectList.addView(rdbtn)
}
AlertDialog.Builder(this, R.style.MyDialogTheme)
.setTitle(R.string.selectStd)
.setPositiveButton("Ok") { dialog, whichButton ->
if (standardDialog.selectSubjectList.checkedRadioButtonId > 0) {
changeSelectedStandardTitle(standardDialog.selectSubjectList.checkedRadioButtonId)
settingHandler.updateSettingsViaKey("default_standard", standardDialog.selectSubjectList.checkedRadioButtonId.toString())
}
dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, whichButton ->
dialog.dismiss()
}
.setView(standardDialog)
.create()
.show()
}
我的 dimens.xml R.dimen.radio_text_size :-
<dimen name="radio_text_size">6sp</dimen>
这里我使用 6 sp 用于单选按钮 Textview 显示不同的屏幕文本大小左侧 Honor 6x ScreenShot 和右侧 Intex
试试这个:
rdbtn.textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,resources.getDimension(R.dimen.radio_text_size), resources.getDisplayMetrics());
如果您查看 TextView#setTextSize(float)
的实现,您会发现它将您提供的值视为 sp
值。当您使用 getDimension
将 sp
值从 dimens.xml
转换为像素值时,当您将该值设置到 TextView 时,它会将该像素值转换为 sp值转换为另一个(不同的)像素值。
您要么只想在代码中使用您的 sp 值,例如
rbbtn.textSize = 6f
或者您想要指定您使用的是像素并使用从维度
中检索到的值
rdbtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.radio_text_size))
我从 dimens.xml 设置了 texview 大小。但它在不同的移动屏幕上表现不同,例如,In honor 6x 设备看起来字体很大,而 intex 移动设备看起来很小但我应用相同的文本大小。
这是我的代码:-
private fun changeStandardDialog(standardList: ArrayList<Category>) {
val factory = LayoutInflater.from(this)
val standardDialog = factory.inflate(R.layout.select_standard_diolog, null)
selectedStandardId = SettingsHandler(this).getSettings("default_standard")
for (item in standardList) {
val rdbtn = RadioButton(this)
rdbtn.id = item.id
rdbtn.text = item.title
if (selectedStandardId.toInt() == item.id) {
rdbtn.isChecked = true
}
rdbtn.textSize = resources.getDimension(R.dimen.radio_text_size)
val textColor = Color.parseColor("#323642")
rdbtn.setButtonTintList(ColorStateList.valueOf(textColor));
rdbtn.setPadding(20, 30, 30, 30)
standardDialog.selectSubjectList.addView(rdbtn)
}
AlertDialog.Builder(this, R.style.MyDialogTheme)
.setTitle(R.string.selectStd)
.setPositiveButton("Ok") { dialog, whichButton ->
if (standardDialog.selectSubjectList.checkedRadioButtonId > 0) {
changeSelectedStandardTitle(standardDialog.selectSubjectList.checkedRadioButtonId)
settingHandler.updateSettingsViaKey("default_standard", standardDialog.selectSubjectList.checkedRadioButtonId.toString())
}
dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, whichButton ->
dialog.dismiss()
}
.setView(standardDialog)
.create()
.show()
}
我的 dimens.xml R.dimen.radio_text_size :-
<dimen name="radio_text_size">6sp</dimen>
这里我使用 6 sp 用于单选按钮 Textview 显示不同的屏幕文本大小左侧 Honor 6x ScreenShot 和右侧 Intex
试试这个:
rdbtn.textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,resources.getDimension(R.dimen.radio_text_size), resources.getDisplayMetrics());
如果您查看 TextView#setTextSize(float)
的实现,您会发现它将您提供的值视为 sp
值。当您使用 getDimension
将 sp
值从 dimens.xml
转换为像素值时,当您将该值设置到 TextView 时,它会将该像素值转换为 sp值转换为另一个(不同的)像素值。
您要么只想在代码中使用您的 sp 值,例如
rbbtn.textSize = 6f
或者您想要指定您使用的是像素并使用从维度
中检索到的值rdbtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.radio_text_size))