Reputation: 318
I am pro grammatically creating buttons and specifying the height and width of each prior to adding it to a Grid View:
Button discountButton = new Button(this, null, 0, R.style.discount_select_button);
discountButton.setText("blah");
discountButton.setWidth(300);
discountButton.setHeight(300);
discountsGrid.addView(discountButton);
The button utilises this style, all items defined within the style get applied except for "layout_margin", why could this be?
<style name="discount_select_button" parent="@android:style/Widget.Button">
<item name="android:layout_margin">10dp</item>
<item name="android:textAlignment">center</item>
<item name="android:textColor">@color/white_light_background_focusable_color</item>
<item name="android:background">@drawable/discount_selection_button_shape</item>
<item name="android:gravity">fill_horizontal</item>
<item name="android:autoSizeMinTextSize">1sp</item>
<item name="android:autoSizeMaxTextSize">25sp</item>
<item name="android:autoSizeTextType">uniform</item>
<item name="android:autoSizeStepGranularity">1sp</item>
</style>
When I apply all the above properties programmatically, all applies successfully but not when defined within a style.
The minimum API level I am targeting is 26
Upvotes: 0
Views: 26
Reputation: 54194
Attributes beginning with layout_
are not actually part of the view, they are part of that view's LayoutParams
object, and they define the behavior of the parent.
If you use that style definition directly in XML, the margins will work, but from Java they will not.
You can create a ViewGroup.MarginLayoutParams
object and set the params.topMargin
field to get your desired result.
Upvotes: 1