问题
I used an attribute layout_marginLeft="30dip"
in a style defined for buttons. When I apply this style individually for each button, the left margin is placed as I wanted.
But then I defined a theme, where I assigned my button style to the attribute android:buttonStyle
and applied it to my project.
However, the left margin property is ignored. Any idea what I must do here?
style.xml as follows:
<style name="btnstyle" parent="@android:style/Widget.Button">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_marginLeft">30dip</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="appstyle" parent="android:Theme">
<item name="android:buttonStyle">@style/btnstyle</item>
</style>
回答1:
It looks like this can be achieved by wrapping an insets tag around the shape.
Like this:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="5dp"
android:insetRight="5dp"
android:insetTop="5dp"
android:insetBottom="5dp">
<shape
android:shape="rectangle">
<solid
android:color="#fff"/>
<stroke
android:width="1dp"
android:color="#333"/>
<corners
android:radius="10dp"/>
</shape>
</inset>
回答2:
There is an awesome answer here on stackoverflow. Basically LayoutParams
(layout_) are used for child views to tell their parent ViewGroup
how they should be positioned. But LayoutParams
are specific to each ViewGroup
type (LinearLayout
, RelativeLayout
, FrameLayout
etc.) so LayoutParams
can not be generalized in this way for a theme.
All this is very unfortunate as you can't use completely generalized styles in a theme but you can still use the style for each specific view where you need it, like following:
<Button style="@style/btnstyle"/>
回答3:
Ever get to the bottom of this? I'm having the same issue; all margin attributes are ignored when applied to a Button or ImageButton via a style within a theme.
However, I've found it does work when the style is applied directly using the "style" attribute on the button. It seems that margins are not part of the "buttonStyle" spec.
回答4:
I would guess this is a bug in Android.
On the other hand, when I started using styles I placed everything inside the style. I noticed that I was creating a style for every widget. Right now, I place position attributes inside a style.
So, width
, height
, padding
and layout
properties in the XML element and the rest of the attributes inside the style.
回答5:
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.
I dont want any Credit of this answer the real hero is here - https://stackoverflow.com/a/13365288/4741746
来源:https://stackoverflow.com/questions/3775587/to-use-layout-marginleft-in-a-button-style-applied-as-a-theme