Last active
February 15, 2016 05:11
-
-
Save truongngoclinh/741d5ad55c1b1b2ad669 to your computer and use it in GitHub Desktop.
The logic is prefer smaller one first then...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// divide two unknown length texts to fit fixed parent width | |
private void adjustEmployeeInfoLayoutWidth() { | |
int dp10 = PlandayUtils.dpToPx(getContext(), 10); | |
int dp15 = PlandayUtils.dpToPx(getContext(), 15); | |
int dp50 = PlandayUtils.dpToPx(getContext(), 50); | |
// civAvatar.width = 50dp + marginLeft = 15dp, nameMarginLeft = 10dp, roleMarginLeft = 15dp, roleMarginRight = 15dp | |
int parentWidth = PlandayUtils.getScreenSize(getContext())[0] - dp50 - 3 * dp15 - dp10; | |
int nameWidth = (int) tvEmployeeName.getPaint().measureText(tvEmployeeName.getText().toString()); | |
int roleWidth = (int) tvRoleName.getPaint().measureText(tvRoleName.getText().toString()); | |
/* The logic is: | |
* - prefer smaller one first, if both above 1/2 parentWidth just separating 2 equal parts | |
* - tvEmployeeName marginLeft = 10dp and tvRoleName marginRight = 15dp | |
* - tvRoleName marginLeft or tvEmployeeName marginRight based on length of both after measuring with text | |
*/ | |
LinearLayout.LayoutParams nameLp, roleLp; | |
if (roleWidth <= parentWidth / 2 && nameWidth <= parentWidth / 2) { | |
nameLp = new LinearLayout.LayoutParams(nameWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
roleLp = new LinearLayout.LayoutParams(roleWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
} else if (roleWidth < nameWidth && roleWidth <= parentWidth / 2 && nameWidth >= parentWidth / 2) { | |
int nameLpWidth = nameWidth < (parentWidth - roleWidth) ? nameWidth : parentWidth - roleWidth; | |
nameLp = new LinearLayout.LayoutParams(nameLpWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
roleLp = new LinearLayout.LayoutParams(roleWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
} else if (nameWidth < roleWidth && nameWidth <= parentWidth / 2 && roleWidth >= parentWidth / 2) { | |
int roleLpWidth = roleWidth < (parentWidth - nameWidth) ? roleWidth : parentWidth - nameWidth; | |
nameLp = new LinearLayout.LayoutParams(nameWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
roleLp = new LinearLayout.LayoutParams(roleLpWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
} else { | |
int childWidth = parentWidth / 2; | |
nameLp = new LinearLayout.LayoutParams(childWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
roleLp = new LinearLayout.LayoutParams(childWidth, LinearLayout.LayoutParams.MATCH_PARENT); | |
} | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
nameLp.setMarginStart(dp10); | |
roleLp.setMarginEnd(dp15); | |
if (roleWidth + nameWidth < parentWidth) { | |
if (nameWidth > roleWidth && nameWidth >= parentWidth / 2) { | |
nameLp.setMarginEnd(parentWidth - nameWidth - roleWidth + dp15); | |
} else { | |
roleLp.setMarginStart(parentWidth - nameWidth - roleWidth + dp15); | |
} | |
} else { | |
if (nameWidth > roleWidth && nameWidth >= parentWidth / 2) { | |
nameLp.setMarginEnd(dp15); | |
} else { | |
roleLp.setMarginStart(dp15); | |
} | |
} | |
} | |
tvEmployeeName.setLayoutParams(nameLp); | |
tvRoleName.setLayoutParams(roleLp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment