I wish to have the default margin for EditText’s be 10dp. Therefore, in my styles.xml file I set up the following:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:editTextStyle">@style/edit_text_default</item>
</style>
<style name="edit_text_default" parent="android:style/Widget.EditText">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
Then in AndroidManifest.xml, I set the applications theme to the one I defined:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
...
The “No Title Bar” aspect of the theme is working. However, the default margin for EditText’s is not, it is still filling the parent. Here is my table view:
<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" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" />
</TableRow>
</TableLayout>
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.
Explanation: Attributes which begin with
layout_are LayoutParams, or one of its subclasses (e.g. MarginLayoutParams).LayoutParamsare used by views to tell their parent ViewGroup how they want to be laid out. Each and everyViewGroupclass implements a nested class that extendsViewGroup.LayoutParams. Therefore,LayoutParamsare specific to theViewGroup‘s type. What this means is that while aTableLayoutand aLinearLayoutmay both havelayout_marginas one of it’sLayoutParams, they are considered to be completely different attributes.So
layout_marginis not just general attribute that can be applied anywhere. It must be applied within the context of aViewGroupthat specifically defines it as a valid argument. A view must be aware of the type of its parentViewGroupwhenLayoutParamsare 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
EditTextview that has been defined with aTableLayoutworks, because the parentViewGroup(theTableLayout) 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.