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

查看:59
本文介绍了如何正确删除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>

当我按下按钮时,我想删除按钮周围的填充物(或者应该将其命名为margin?请参阅我最底部的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中删除按钮周围的填充?/a>

I look at How to remove padding around buttons in 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?

我真的不知道该称其为padding还是margin.我希望实现的是,当我们按底部区域时,按视觉效果更改应该覆盖整个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天全站免登陆