|
23 | 23 | */
|
24 | 24 | public class CustomButton extends LinearLayout implements View.OnClickListener {
|
25 | 25 |
|
| 26 | + //WARNING: If you want to change it, you should change it in attr xml too. |
26 | 27 | public static final int LEFT = 0;
|
27 | 28 | public static final int TOP = 1;
|
28 | 29 | public static final int RIGHT = 2;
|
@@ -92,7 +93,7 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int backgrou
|
92 | 93 | shapeRadius = 0;
|
93 | 94 | imageScaleType = ImageView.ScaleType.FIT_CENTER;
|
94 | 95 |
|
95 |
| - if (drawableNormal != null) drawablePosition = LEFT; |
| 96 | + if (drawableNormal != null) drawablePosition = LEFT; //default position |
96 | 97 | else drawablePosition = -1;
|
97 | 98 |
|
98 | 99 | setContent(context);
|
@@ -226,26 +227,17 @@ private void setContent(Context context) {
|
226 | 227 | setTextPadding(textPaddingArray);
|
227 | 228 |
|
228 | 229 | 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); |
233 | 236 | layoutParamsText.weight = (text != null) ? ((textWeight == 0) ? 1 : textWeight) : 0;
|
234 | 237 | textWeight = (int) layoutParamsText.weight;
|
235 |
| - layoutParamsImage.weight = (drawableNormal != null) ? ((imageWeight == 0) ? 1 : imageWeight) : 0; |
| 238 | + layoutParamsImage.weight = (imageWeight == 0) ? 1 : imageWeight; |
236 | 239 | 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); |
249 | 241 | imageContainer.setLayoutParams(layoutParamsImage);
|
250 | 242 |
|
251 | 243 | if (drawablePressed == null) drawablePressed = drawableNormal;
|
@@ -287,6 +279,14 @@ private void setContent(Context context) {
|
287 | 279 | container.addView(imageContainer);
|
288 | 280 | break;
|
289 | 281 | }
|
| 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 | + } |
290 | 290 | }
|
291 | 291 |
|
292 | 292 | addView(container);
|
|
0 commit comments