Android 设计库 - 浮动操作按钮填充/边距问题 [英] Android Design Library - Floating Action Button Padding/Margin Issues

查看:41
本文介绍了Android 设计库 - 浮动操作按钮填充/边距问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google 设计库中的新 FloatingActionButton,但遇到了一些奇怪的填充/边距问题.此图片(启用了开发者布局选项)来自 API 22.

I'm using the new FloatingActionButton from the Google Design Library and I am getting some strange padding/margin problems. This image (with developer layout options on) is from API 22.

来自 API 17.

这是 XML

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_gravity="bottom|right"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-32dp"
        android:src="@drawable/ic_action_add"
        app:fabSize="normal"
        app:elevation="4dp"
        app:borderWidth="0dp"
        android:layout_below="@+id/header"/>

为什么 API 17 中的 FAB 的填充/边距要大得多?

Why is the FAB in API 17 so much larger padding/margin wise?

推荐答案

更新(2016 年 10 月):

现在正确的解决方案是将 app:useCompatPadding="true" 放入您的 FloatingActionButton.这将使不同 API 版本之间的填充保持一致.但是,这似乎仍然使默认边距稍微关闭,因此您可能需要调整它们.但至少不再需要特定于 API 的样式.

The correct solution now is to put app:useCompatPadding="true" into your FloatingActionButton. This will make the padding consistent between different API versions. However, this still seems to make the default margins off by a little bit, so you may need to adjust those. But at least there's no further need for API-specific styles.

上一个答案:

您可以使用特定于 API 的样式轻松完成此操作.在您的普通 values/styles.xml 中,输入如下内容:

You can accomplish this easily using API-specific styles. In your normal values/styles.xml, put something like this:

<style name="floating_action_button">
    <item name="android:layout_marginLeft">0dp</item>
    <item name="android:layout_marginTop">0dp</item>
    <item name="android:layout_marginRight">8dp</item>
    <item name="android:layout_marginBottom">0dp</item>
</style>

然后在 values-v21/styles.xml 下,使用这个:

and then under values-v21/styles.xml, use this:

<style name="floating_action_button">
    <item name="android:layout_margin">16dp</item>
</style>

并将样式应用到您的 FloatingActionButton:

and apply the style to your FloatingActionButton:

<android.support.design.widget.FloatingActionButton
...
style="@style/floating_action_button"
...
/>

正如其他人所指出的,在 API <20 中,按钮呈现自己的阴影,这会增加视图的整体逻辑宽度,而在 API >=20 中,它使用新的 Elevation 参数,这些参数对视图宽度.

As others have noted, in API <20, the button renders its own shadow, which adds to the overall logical width of the view, whereas in API >=20 it uses the new Elevation parameters which don't contribute to the view width.

这篇关于Android 设计库 - 浮动操作按钮填充/边距问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆