diff --git a/README.md b/README.md index 420881c..37fe01b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ allprojects { 然后在需要使用的模块的build.gradle中添加以下代码 ```groovy dependencies { - implementation 'com.github.Brook007:DependencyLayout:1.0.1' + implementation 'com.github.Brook007:DependencyLayout:1.0.3' } ``` diff --git a/library/src/main/java/com/brook/app/android/supportlibrary/util/Util.java b/library/src/main/java/com/brook/app/android/supportlibrary/util/Util.java index 4d964e8..ccf2156 100644 --- a/library/src/main/java/com/brook/app/android/supportlibrary/util/Util.java +++ b/library/src/main/java/com/brook/app/android/supportlibrary/util/Util.java @@ -180,8 +180,8 @@ public static float dp2px(float dpVal) { * @param spVal * @return */ - public static int sp2px(float spVal) { + public static float sp2px(float spVal) { final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity; - return (int) (spVal * fontScale + 0.5f); + return (spVal * fontScale + 0.5f); } } diff --git a/library/src/main/java/com/brook/app/android/supportlibrary/view/DependencyLayout.java b/library/src/main/java/com/brook/app/android/supportlibrary/view/DependencyLayout.java index e59aaa6..a9db13f 100644 --- a/library/src/main/java/com/brook/app/android/supportlibrary/view/DependencyLayout.java +++ b/library/src/main/java/com/brook/app/android/supportlibrary/view/DependencyLayout.java @@ -942,13 +942,16 @@ public LayoutParams(Context context, AttributeSet attrs) { private float calculationSystem(TypedArray typedArray, int attr, DisplayMetrics displayMetrics) { String text = Util.getText(typedArray, attr); String value = Util.getValue(text); - String unit = text.substring(value.length(), text.length()).toLowerCase(); - if ("dip".equals(unit)) { - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Float.valueOf(value), displayMetrics); + String unit = text.substring(value.length()).toLowerCase(); + if ("dip".equals(unit) || "dp".equals(unit)) { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Util.toFloat(value), displayMetrics); } else if ("px".endsWith(unit)) { - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, Float.valueOf(value), displayMetrics); + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, Util.toFloat(value), displayMetrics); + } else if ("sp".endsWith(unit)) { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, Util.toFloat(value), displayMetrics); + } else { + return Util.toFloat(value); } - return Float.valueOf(value); } /**