Skip to content

Commit 5fe8c3a

Browse files
refactor(*): 优化TitleBarView
1 parent a3f2d85 commit 5fe8c3a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lite/src/main/java/com/pengxh/kt/lite/widget/TitleBarView.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import android.widget.RelativeLayout
1111
import android.widget.TextView
1212
import com.pengxh.kt.lite.R
1313
import com.pengxh.kt.lite.extensions.dp2px
14-
import com.pengxh.kt.lite.extensions.sp2px
14+
import com.pengxh.kt.lite.extensions.getScreenDensity
1515

1616

1717
/**
@@ -47,7 +47,8 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
4747
textView.text = title
4848
textView.isSingleLine = true
4949
textView.ellipsize = TextUtils.TruncateAt.END
50-
textView.textSize = titleSize.sp2px(context)
50+
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
51+
textView.textSize = titleSize / context.getScreenDensity()
5152
textView.gravity = Gravity.CENTER
5253
textView.setTextColor(titleColor)
5354
titleParams.addRule(CENTER_IN_PARENT, TRUE)
@@ -78,7 +79,8 @@ class TitleBarView constructor(context: Context, attrs: AttributeSet) :
7879
textView.text = title
7980
textView.isSingleLine = true
8081
textView.ellipsize = TextUtils.TruncateAt.END
81-
textView.textSize = titleSize.sp2px(context)
82+
//textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
83+
textView.textSize = titleSize / context.getScreenDensity()
8284
textView.gravity = Gravity.CENTER
8385
textView.setTextColor(titleColor)
8486
titleParams.leftMargin = textMargin

0 commit comments

Comments
 (0)