28

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

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

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

5 回答 5

14

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

像这样:

<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>
于 2014-01-04T00:11:42.810 回答
11

stackoverflow 上有一个很棒的答案。基本上LayoutParams(layout_) 用于子视图,以告诉其父视图ViewGroup应如何定位。但LayoutParams特定于每种ViewGroup类型(LinearLayout、等),因此RelativeLayout不能以这种方式概括为一个主题。FrameLayoutLayoutParams

所有这一切都非常不幸,因为您不能在主题中使用完全通用的样式,但您仍然可以在需要的每个特定视图中使用该样式,如下所示:

<Button style="@style/btnstyle"/>
于 2013-03-21T13:10:29.073 回答
3

有没有追根究底?我有同样的问题;当通过主题中的样式应用于 Button 或 ImageButton 时,所有边距属性都会被忽略。

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

于 2011-01-10T10:36:01.983 回答
2

我猜这是Android中的一个错误。

另一方面,当我开始使用样式时,我将所有内容都放在样式中。我注意到我正在为每个小部件创建一个样式。现在,我将位置属性放在样式中。

因此,widthXML元素中的height、、paddinglayout属性以及样式中的其余属性。

于 2011-11-25T17:32:35.753 回答
0

简短回答:如果您在自定义样式中指定 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_marginit's 之一LayoutParams,但它们被认为是完全不同的属性。

因此layout_margin,不仅仅是可以在任何地方应用的通用属性。它必须在ViewGroup明确将其定义为有效参数的 a 的上下文中应用。应用视图时必须知道其父视图的类型。ViewGroupLayoutParams

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

资料来源:

布局参数的Android 文档。

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

另外,StackOverflow 用户inazaruk对此问题的回答。

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

于 2017-08-01T12:57:40.400 回答