要在作为主题应用的按钮样式中使用layout_marginLeft吗?

时间:2021-12-13 18:24:38

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.

我在为按钮定义的样式中使用了属性layout_marginLeft =“30dip”。当我为每个按钮单独应用此样式时,左边距按我的意愿放置。

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?

但后来我定义了一个主题,我将我的按钮样式分配给属性android:buttonStyle并将其应用到我的项目中。但是,左边距属性将被忽略。知道我必须在这做什么吗?

style.xml as follows:

style.xml如下:

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

5 个解决方案

#1


13  

It looks like this can be achieved by wrapping an insets tag around the shape.

看起来这可以通过在形状周围包裹insets标签来实现。

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


10  

There is an awesome answer here on *. 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.

*上有一个很棒的答案。基本上,LayoutParams(layout_)用于子视图,以告诉他们的父ViewGroup如何定位它们。但LayoutParams特定于每个ViewGroup类型(LinearLayout,RelativeLayout,FrameLayout等),因此LayoutParams不能以这种方式为主题推广。

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


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.

永远深究这个?我有同样的问题;通过主题中的样式应用于Button或ImageButton时,将忽略所有边距属性。

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.

但是,我发现当使用按钮上的“style”属性直接应用样式时它确实有效。似乎边距不是“buttonStyle”规范的一部分。

#4


2  

I would guess this is a bug in Android.

我猜这是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.

因此,XML元素中的宽度,高度,填充和布局属性以及样式中的其余属性。

#5


0  

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.

简答:如果您在自定义样式中指定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>

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.

Explanation:以layout_开头的属性是LayoutParams或其子类之一(例如MarginLayoutParams)。视图使用LayoutParams告诉其父ViewGroup如何布局它们。每个ViewGroup类都实现一个扩展ViewGroup.LayoutParams的嵌套类。因此,LayoutParams特定于ViewGroup的类型。这意味着虽然T​​ableLayout和LinearLayout都可以将layout_margin作为其LayoutParams之一,但它们被认为是完全不同的属性。

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.

所以layout_margin不仅仅是可以在任何地方应用的一般属性。它必须在ViewGroup的上下文中应用,ViewGroup特别将其定义为有效参数。应用LayoutParams时,视图必须知道其父ViewGroup的类型。

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.

在样式中指定layout_margin(包括主题中的样式)并尝试将该主题应用于应用程序/活动将导致布局属性被删除,因为尚未指定ViewGroup父级,因此参数无效。但是,将样式应用于已使用TableLayout定义的EditText视图,因为父ViewGroup(TableLayout)已知。

Sources:

Android documentation on Layout Parameters.

有关布局参数的Android文档。

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

Android框架工程师和*用户adamp对此问题的回答。

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

此外,*用户inazaruk回答了这个问题。

I dont want any Credit of this answer the real hero is here - https://*.com/a/13365288/4741746

我不想要这个答案的任何信用真正的英雄在这里 - https://*.com/a/13365288/4741746

#1


13  

It looks like this can be achieved by wrapping an insets tag around the shape.

看起来这可以通过在形状周围包裹insets标签来实现。

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


10  

There is an awesome answer here on *. 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.

*上有一个很棒的答案。基本上,LayoutParams(layout_)用于子视图,以告诉他们的父ViewGroup如何定位它们。但LayoutParams特定于每个ViewGroup类型(LinearLayout,RelativeLayout,FrameLayout等),因此LayoutParams不能以这种方式为主题推广。

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


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.

永远深究这个?我有同样的问题;通过主题中的样式应用于Button或ImageButton时,将忽略所有边距属性。

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.

但是,我发现当使用按钮上的“style”属性直接应用样式时它确实有效。似乎边距不是“buttonStyle”规范的一部分。

#4


2  

I would guess this is a bug in Android.

我猜这是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.

因此,XML元素中的宽度,高度,填充和布局属性以及样式中的其余属性。

#5


0  

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.

简答:如果您在自定义样式中指定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>

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.

Explanation:以layout_开头的属性是LayoutParams或其子类之一(例如MarginLayoutParams)。视图使用LayoutParams告诉其父ViewGroup如何布局它们。每个ViewGroup类都实现一个扩展ViewGroup.LayoutParams的嵌套类。因此,LayoutParams特定于ViewGroup的类型。这意味着虽然T​​ableLayout和LinearLayout都可以将layout_margin作为其LayoutParams之一,但它们被认为是完全不同的属性。

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.

所以layout_margin不仅仅是可以在任何地方应用的一般属性。它必须在ViewGroup的上下文中应用,ViewGroup特别将其定义为有效参数。应用LayoutParams时,视图必须知道其父ViewGroup的类型。

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.

在样式中指定layout_margin(包括主题中的样式)并尝试将该主题应用于应用程序/活动将导致布局属性被删除,因为尚未指定ViewGroup父级,因此参数无效。但是,将样式应用于已使用TableLayout定义的EditText视图,因为父ViewGroup(TableLayout)已知。

Sources:

Android documentation on Layout Parameters.

有关布局参数的Android文档。

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

Android框架工程师和*用户adamp对此问题的回答。

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

此外,*用户inazaruk回答了这个问题。

I dont want any Credit of this answer the real hero is here - https://*.com/a/13365288/4741746

我不想要这个答案的任何信用真正的英雄在这里 - https://*.com/a/13365288/4741746