简短回答:如果您在自定义样式中指定 layout_margin,则必须将此样式显式应用于您希望具有指定边距的每个单独的视图(如下面的代码示例所示)。在主题中包含此样式并将其应用于您的应用程序或活动将不起作用。
<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>
说明:layout_
以LayoutParams或其子类之一开头的属性(例如MarginLayoutParams)。LayoutParams
被视图用来告诉他们的父ViewGroup他们想要如何布局。每个ViewGroup
类都实现了一个可扩展的嵌套类ViewGroup.LayoutParams
。因此,LayoutParams
都是特定于ViewGroup
's 的类型。这意味着虽然 aTableLayout
和 aLinearLayout
可能都具有layout_margin
it's 之一LayoutParams
,但它们被认为是完全不同的属性。
因此layout_margin
,不仅仅是可以在任何地方应用的通用属性。它必须在ViewGroup
明确将其定义为有效参数的 a 的上下文中应用。应用视图时必须知道其父视图的类型。ViewGroup
LayoutParams
在样式中指定 layout_margin,包括主题中的样式,并尝试将该主题应用于应用程序/活动将导致布局属性被删除,因为尚未指定 ViewGroup 父级,因此参数无效。但是,将样式应用于EditText
已使用 a 定义的视图是TableLayout
有效的,因为父级ViewGroup
(the TableLayout
) 是已知的。
资料来源:
布局参数的Android 文档。
Android 框架工程师和 StackOverflow 用户adamp对此问题的回答。
另外,StackOverflow 用户inazaruk对此问题的回答。
我不想要这个答案的任何信用真正的英雄在这里 - https://stackoverflow.com/a/13365288/4741746