【问题标题】:Android API 21 ignores layout_marginEnd and uses layout_marginRight instead in EditTextAndroid API 21 忽略 layout_marginEnd 并在 EditText 中使用 layout_marginRight
【发布时间】:2015-03-30 12:07:53
【问题描述】:

这是我的 EditText。 我的问题是 Android 忽略了 Lollipop 及更高版本中的 layout_marginEnd 并使用 layout_marginRight 代替。 请看以下两张截图:

<EditText
        android:id="@+id/foo_count_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/foo_container"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="100dp"
        android:layout_marginTop="-6dp"
        android:hint="@string/foo_default_value"
        android:imeOptions="actionDone"
        android:inputType="number"/>

可以看出,在 API 21(左)中,边距为 100dp,取自 layout_marginRight 而不是 layout_marginEnd。

编辑: @ErnirErlingsson 成功了:需要在清单中启用 android:supportsRtl 才能使用 layout_marginEnd 而不是 layout_marginRight。 谢谢。

编辑 2: 但请注意,在 API 17(右截图)中,右边距取自 layout_marginEnd 而 android:supportsRtl 为 false。

【问题讨论】:

    标签: android android-layout android-5.0-lollipop margins


    【解决方案1】:

    一个替换另一个,如果你决定现在或将来支持 RTL,你应该总是使用 marginStart 和 marginEnd 而不是使用 marginRight 和 marginLeft,否则你可以使用后者。

    http://developer.android.com/about/versions/android-4.2.html#RTL

    根据相应的docs RTL 支持应该默认禁用,因此应该使用左/右边距。据我所知,这在您随问题发布的屏幕截图中可见的操作系统版本之间没有区别,因此究竟为什么会发生这种情况有点神秘。我的第一个猜测是这与模拟器有关,我会在具有相应操作系统版本的实际设备上确认这一点。

    编辑

    当支持较旧的操作系统版本时,您确实需要将两者都添加到布局中,但要确保使用 marginStartmarginEnd,您需要将 android:supportsRtl 属性设置为 true .查看我在上面发布的第一个链接了解更多信息。

    【讨论】:

    • 我知道在 API >=17 中 end 替换了 right 但是我必须保留旧的,因为我仍然支持 API14。此外,我已经在实际的 Lollipop 设备上尝试过这个应用程序,和这里一样,Lollipop 忽略了新的 layout_marginEnd 属性。
    • 有谁知道以后不再需要 compat 时是否有办法清除所有重复项?把这一切都做 2 倍有点糟糕......
    • Lint 可能至少会在您将最低 API 级别提高到 17 或更高时将您指向文件,但要自动删除它,您可能必须恢复为命令行脚本。
    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多