Java 如何减少 Android 按钮对象中文本周围的内边距?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16394252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:39:40  来源:igfitidea点击:

How do I reduce the inner padding around the text within an Android button object?

javaandroidxmlmobileuser-interface

提问by Imran NZ

Example buttons

示例按钮

So, at the moment I have a button which looks like the first image above. How do I reduce the padding around the text inside the button itself (To look more like the second image)?

所以,目前我有一个按钮,看起来像上面的第一张图片。如何减少按钮本身内部文本周围的填充(看起来更像第二张图片)?

Layout width and height is set as:

布局宽度和高度设置为:

android:layout_width="match_parent"
android:layout_height="wrap_content"

The custom style shape has parameters"

自定义样式形状有参数"

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">

With the rest just being color attributes and radii values.

其余的只是颜色属性和半径值。

Just to make it clear, I want the frame of the button to hug the "Login" text closer.

只是为了清楚起见,我希望按钮的框架更靠近“登录”文本。

All help and feedback is greatly appreciated. Thanks.

非常感谢所有帮助和反馈。谢谢。

采纳答案by Steven

It took me forever to find this but the "padding" around the text in a button isn't really padding at all. The default Widget.Button style includes a minHeight property. Changing minHeight on the button will allow you to adjust padding as expected.

我花了很长时间才找到这个,但是按钮中文本周围的“填充”根本不是真正的填充。默认的 Widget.Button 样式包括 minHeight 属性。更改按钮上的 minHeight 将允许您按预期调整填充。

<Button
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/test"
        android:textColor="@color/black"
        android:minHeight="40dip"/>


<style name="Widget.Holo.Button" parent="Widget.Button">
    <item name="android:background">@android:drawable/btn_default_holo_dark</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    <item name="android:textColor">@android:color/primary_text_holo_dark</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">64dip</item>
</style>

回答by Neoh

If you want a smaller top and bottom padding, use this:

如果您想要较小的顶部和底部填充,请使用:

 android:paddingTop="2dp"
 android:paddingBottom="2dp"

回答by Qadir Hussain

try this in your custom shape.xml file

在您的自定义 shape.xml 文件中试试这个

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="1dp"
android:padding="1dp">

also you can make change in your button's xml

您也可以更改按钮的 xml

android:layout_width="wrap_content"
android:layout_height="wrap_content"

回答by Shihab Uddin

You can use Borderlessstyle in AppCompatButtonlike below. Also use background color your preferred.

您可以在AppCompatButton 中使用无边框样式,如下所示。还可以使用您喜欢的背景颜色。

Widget.AppCompat.Button.Borderless.Colored

Widget.AppCompat.Button.Borderless.Colored

Button code

按钮代码

<androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_visa_next"
        android:background="@color/colorPrimary"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_normal"
        android:text="@string/next"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Output:

输出:

enter image description here

在此处输入图片说明