Android - Margins Specified in Custom Style Not Taking Effect

Android - margins specified in custom style not taking effect

Short Answer: If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" style="@style/edit_text_default" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" style="@style/edit_text_default" />
</TableRow>
</TableLayout>

Explanation: Attributes which begin with layout_ are LayoutParams, or one of its subclasses (e.g. MarginLayoutParams). LayoutParams are used by views to tell their parent ViewGroup how they want to be laid out. Each and every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. Therefore, LayoutParams are specific to the ViewGroup's type. What this means is that while a TableLayout and a LinearLayout may both have layout_margin as one of it's LayoutParams, they are considered to be completely different attributes.

So layout_margin is not just general attribute that can be applied anywhere. It must be applied within the context of a ViewGroup that specifically defines it as a valid argument. A view must be aware of the type of its parent ViewGroup when LayoutParams are applied.

Specifying layout_margin in a style, including that style in a theme, and attempting to apply that theme to an application/activity will result in the layout attributes being dropped, because no ViewGroup parent has been specified yet and so the arguments are invalid. However, applying the style to an EditText view that has been defined with a TableLayout works, because the parent ViewGroup (the TableLayout) is known.

Sources:

Android documentation on Layout Parameters.

Answer given to this question by Android framework engineer and StackOverflow user adamp.

Also, answer given to this question by StackOverflow user inazaruk.

Anko ignoring layout_margin defined in style

This is not Anko problem, this is how Android works:

If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.

This is because attributes which begin with layout_ are LayoutParams, or as in this example its MarginLayoutParams. Each ViewGroup have it's own LayoutParams implementation. And so layout_margin is not just general attribute that can be applied anywhere. It must be applied within the context of a ViewGroup that specifically defines it as a valid argument.

Look here for more.

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);

Android ConstraintLayout Margins not working correctly

I see one definite problem and one potential problem.

The definite problem is that your textView3 specifies these attributes:

android:layout_margin="8dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"

This does not set the top/start to 16dp and the end/bottom to 8dp. It simply causes the marginTop and marginStart attributes to be ignored.

The potential problem is that you specify

android:layout_marginStart="16dp"

but do not also specify marginLeft. If you want to support pre-API 17 devices, you need to specify both marginStart and marginLeft (assuming you remove the margin attribute).

Margin attributes not working inside a custom DialogFragment

In some cases, the very outer margin just does not work or gets cut to zero on some Android API versions. It mostly happens in AdapterViews and Dialogs.
If you use padding instead, it'll work on all Android version.

Cannot change button's layout_margin from styles?

I was just facing the same problem and the solution with margins is in defining style on every button in every layout like this:

<TableRow
android:layout_weight="1"
android:orientation="horizontal"
android:baselineAligned="false"
android:clickable="false"
android:layout_height="fill_parent"
android:layout_width="match_parent">

<Button
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="1"
android:id="@+id/button1"
android:layout_weight="0.06"
android:clickable="true" />

<Button
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="2"
android:id="@+id/button2"
android:layout_weight="0.06" />

<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="3"
android:id="@+id/button3"
android:layout_weight="0.06"
android:layout_marginBottom="2dp" // This works
android:layout_marginTop="2dp" />
</TableRow>
//...

So, you don't even need to add it to theme in this case.

Source: https://stackoverflow.com/a/13365288/3564556



Related Topics



Leave a reply



Submit