Skip to content

Commit 27abf87

Browse files
committed
Library set content refactor.
1 parent 3913af9 commit 27abf87

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

library/src/main/java/pl/sigmapoint/customview/CustomButton.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
public class CustomButton extends LinearLayout implements View.OnClickListener {
2525

26+
//WARNING: If you want to change it, you should change it in attr xml too.
2627
public static final int LEFT = 0;
2728
public static final int TOP = 1;
2829
public static final int RIGHT = 2;
@@ -92,7 +93,7 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int backgrou
9293
shapeRadius = 0;
9394
imageScaleType = ImageView.ScaleType.FIT_CENTER;
9495

95-
if (drawableNormal != null) drawablePosition = LEFT;
96+
if (drawableNormal != null) drawablePosition = LEFT; //default position
9697
else drawablePosition = -1;
9798

9899
setContent(context);
@@ -226,26 +227,17 @@ private void setContent(Context context) {
226227
setTextPadding(textPaddingArray);
227228

228229
LayoutParams layoutParamsText;
229-
LayoutParams layoutParamsImage = null;
230-
if (drawablePosition >= 0 || drawablePosition <= 3) {
231-
layoutParamsText = new LinearLayout.LayoutParams((drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT, (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0);
232-
layoutParamsImage = new LinearLayout.LayoutParams((drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT, (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0);
230+
LayoutParams layoutParamsImage;
231+
if (drawablePosition >= 0 || drawablePosition <= 3 || drawableNormal != null) {
232+
int width = (drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT;
233+
int height = (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0;
234+
layoutParamsText = new LinearLayout.LayoutParams(width, height);
235+
layoutParamsImage = new LinearLayout.LayoutParams(width, height);
233236
layoutParamsText.weight = (text != null) ? ((textWeight == 0) ? 1 : textWeight) : 0;
234237
textWeight = (int) layoutParamsText.weight;
235-
layoutParamsImage.weight = (drawableNormal != null) ? ((imageWeight == 0) ? 1 : imageWeight) : 0;
238+
layoutParamsImage.weight = (imageWeight == 0) ? 1 : imageWeight;
236239
imageWeight = (int) layoutParamsImage.weight;
237-
} else {
238-
layoutParamsText = new LinearLayout.LayoutParams(getLayoutParams().width, getLayoutParams().height);
239-
}
240-
241-
textView.setLayoutParams(layoutParamsText);
242-
243-
if (drawablePosition < 0 || drawablePosition > 3 || drawableNormal == null) {
244-
if (text != null) {
245-
textView.setGravity(Gravity.CENTER);
246-
container.addView(textView);
247-
}
248-
} else {
240+
textView.setLayoutParams(layoutParamsText);
249241
imageContainer.setLayoutParams(layoutParamsImage);
250242

251243
if (drawablePressed == null) drawablePressed = drawableNormal;
@@ -287,6 +279,14 @@ private void setContent(Context context) {
287279
container.addView(imageContainer);
288280
break;
289281
}
282+
283+
} else {
284+
layoutParamsText = new LinearLayout.LayoutParams(getLayoutParams().width, getLayoutParams().height);
285+
textView.setLayoutParams(layoutParamsText);
286+
if (text != null) {
287+
textView.setGravity(Gravity.CENTER);
288+
container.addView(textView);
289+
}
290290
}
291291

292292
addView(container);

0 commit comments

Comments
 (0)