Layout problem with button margin

Remember, android:layout_* attributes are LayoutParams. They are arguments to the parent and affect how the parent will perform layout on that view. You’re specifying layout_margin attributes on your buttons, but they’re getting ignored. Here’s why:

Since LayoutParams are specific to the parent view type, you need to supply an instance of the correct parent type when you inflate layouts using a LayoutInflater or else layout_ attributes on the top-level view in the layout will be dropped. (The inflater would have no idea what type of LayoutParams to generate.)

Since buttonList is your intended parent for the button views, change your inflate line to this:

btn = (Button) layoutInflater.inflate(R.layout.button, buttonList, false);

Leave a Comment