如何正确删除 Android 中按钮周围的填充(或边距?)? [英] How to properly remove padding (or margin?) around buttons in Android?

查看:50
本文介绍了如何正确删除 Android 中按钮周围的填充(或边距?)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下底部登录按钮.

Currently, I have the following bottom log in button.

XML 看起来像这样

<LinearLayout
    android:background="?attr/welcomeBottomNavBarBackground"
    android:orientation="horizontal"
    android:id="@+id/sign_in_bottom_nav_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    <Button
        style="?android:attr/buttonBarButtonStyle"
        android:id="@+id/sign_in_button"
        android:layout_width="0dp"
        android:width="0dp"
        android:layout_weight="1.0"
        android:layout_height="48dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:enabled="true"
        android:textAllCaps="true"
        android:text="@string/log_in" />
</LinearLayout>

我想在按下按钮时删除按钮周围的填充(或者我应该称之为边距?请参阅我最底部的 p/s 部分).

I would like to remove the padding (Or should I call it margin? Please refer to my bottom most p/s section) around button when it is being pressed.

我看如何在Android中删除按钮周围的填充?

我试过了

<Button
    ...
    ...
    android:minHeight="0dp"
    android:minWidth="0dp" />

它不起作用,也没有效果.

It doesn't work and has no effect.

我再试试

<Button
    ...
    ...
    android:background="@null"
    android:minHeight="0dp"
    android:minWidth="0dp" />

按下时不再填充.但是,材质设计的压制视觉效果也会消失.

No more padding when pressed. However, the material designed pressed visual effect will gone too.

我可以知道在按下期间去除按钮填充的最佳方法是什么,同时保留材料设计的按下视觉效果吗?

May I know what is the best way to remove button padding during pressed, yet retain the material designed pressed visual effect?

我真的不知道我应该称之为填充还是边距.我希望实现的是,当我们按下底部区域时,按下视觉效果变化应该覆盖整个 100% 底部栏区域(@+id/sign_in_bottom_nav_bar),而不是当前的 95% 底部酒吧区域.

I don't really know whether I should call it padding or margin. What I wish to achieve is that, when we press on the bottom region, press visual effect change should be covered entire 100% bottom bar region (@+id/sign_in_bottom_nav_bar), instead of current 95% bottom bar region.

推荐答案

标准按钮不应该在全宽下使用,这就是您遇到这种情况的原因.

A standard button is not supposed to be used at full width which is why you experience this.

如果您查看材料设计 - 按钮样式 你会看到一个按钮有一个 48dp 的高度点击区域,但由于某种原因会显示为 36dp 的高度.

If you have a look at the Material Design - Button Style you will see that a button has a 48dp height click area, but will be displayed as 36dp of height for...some reason.

这是您看到的背景轮廓,它不会覆盖按钮本身的整个区域.
它有圆角和一些内边距,应该可以自行点击,包裹其内容,而不是跨越屏幕底部的整个宽度.

This is the background outline you see, which will not cover the whole area of the button itself.
It has rounded corners and some padding and is supposed to be clickable by itself, wrap its content, and not span the whole width at the bottom of your screen.

如上所述,您想要的是不同的背景.不是标准按钮,而是具有这种漂亮涟漪效果的可选项目的背景.

As mentioned above, what you want is a different background. Not a standard button, but a background for a selectable item with this nice ripple effect.

对于这个用例,有 ?selectableItemBackground 主题属性,您可以将其用于背景(尤其是在列表中).
它将添加平台标准波纹(或 <21 上的一些颜色状态列表),并将使用您当前的主题颜色.

For this use case there is the ?selectableItemBackground theme attribute which you can use for your backgrounds (especially in lists).
It will add a platform standard ripple (or some color state list on < 21) and will use your current theme colors.

对于您的用例,您可能只使用以下内容:

For your usecase you might just use the following:

<Button
    android:id="@+id/sign_in_button"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"
    android:background="?attr/selectableItemBackground" />
                   <!--  / that's all -->

如果您的视图是唯一的并且跨越整个屏幕,则也无需添加布局权重

如果您对背景应该是什么样子有一些不同的想法,您必须自己创建一个自定义的可绘制对象,并在那里管理颜色和状态.

If you have some different idea on what your background should look like you have to create a custom drawable yourself, and manage color and state there.

这篇关于如何正确删除 Android 中按钮周围的填充(或边距?)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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