【问题标题】:Android - margins specified in custom style not taking effectAndroid - 自定义样式中指定的边距未生效
【发布时间】:2012-10-21 11:26:46
【问题描述】:

我希望 EditText 的默认边距为 10dp。因此,在我的 styles.xml 文件中,我设置了以下内容:

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

然后在 AndroidManifest.xml 中,我将应用程序主题设置为我定义的主题:

<application
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/MyTheme" >
...

主题的“无标题栏”方面正在发挥作用。但是,EditText 的默认边距不是,它仍在填充父级。这是我的表格视图:

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

【问题讨论】:

    标签: android styles themes


    【解决方案1】:

    您没有在清单中使用正确的主题名称。尝试将其更改为:

    <application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/MyTheme" >
    

    【讨论】:

    • 对不起,这是我在帖子中的错字。在我的实际项目中它们是相同的。好建议:/
    【解决方案2】:

    简短回答:如果您在自定义样式中指定 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 的类型。这意味着虽然TableLayoutLinearLayout 可能都将layout_margin 作为LayoutParams 之一,但它们被认为是完全不同的属性。

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

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

    来源:

    Layout Parameters 上的 Android 文档。

    Android 框架工程师和 StackOverflow 用户 adampthis question 的答案。

    另外,StackOverflow 用户 inazarukthis question 的答案。

    【讨论】:

    • W...T...F??为什么?为什么?为什么?!?! ...请原谅我...我对 Android 开发的所有怪癖都失去了理智。
    • 这个答案有些不尽人意——即使父 ViewGroup 和子 View 在同一个布局 xml 文件中,布局边距仍然没有应用。在这种情况下,inflater 确实知道父 ViewGroup 的具体类型(因为它只是对它进行了充气)并且还知道要应用的样式(因为应用了其他样式属性)。
    【解决方案3】:

    以防万一它对其他人有用,我想在这里带来the answer by Oliv to another SO question。 我试图为所有按钮添加边距,发现 layout_margin 不起作用。

    主题:

    <style name="FooThemeStyle" parent="Base.Theme.AppCompat">
        <item name="android:buttonStyle">@style/FooButton</item>
        <item name="buttonStyle">@style/FooButton</item>
    </style>
    

    按钮样式:

    <style name="FooButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/button_background</item>
    </style>
    

    这里是 button_background.xml,其中the answer by Oliv to another SO question 用于将边距设置为 2dp::

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="2dp"
            android:left="2dp"
            android:right="2dp"
            android:top="2dp">
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
              ...
            </selector>
        </item>
    </layer-list>
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2017-09-14
      • 2013-01-22
      • 1970-01-01
      • 2013-07-19
      • 2017-09-21
      • 2014-08-23
      • 1970-01-01
      • 2019-05-23
      相关资源
      最近更新 更多