简体   繁体   English

Android 中按钮周围不需要的填充或边距

[英]Unwanted padding or margin around buttons in Android

Here is what the app currently looks like when I run it:这是我运行该应用程序时当前的样子:

预览应用

As you can see there is some sort of margin/padding around the buttons.如您所见,按钮周围有某种边距/填充。 I want them to ideally go from edge to edge.我希望他们理想地从边缘走到边缘。 I've tried to change the margin, padding, and constraints.我尝试更改边距、填充和约束。

Here is what the XML currently looks like for the two buttons (in a LinearLayout).下面是两个按钮(在 LinearLayout 中)的 XML 当前的样子。

<Button
    android:id="@+id/cameraPageButton"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:text="@string/cameraPageButton"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:backgroundTint="@color/CallPageButton"/>

<Button
    android:id="@+id/messagePageButton"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:backgroundTint="@color/MessagePageButton"
    android:text="@string/messagePageButton"
    app:layout_constraintBottom_toTopOf="@id/cameraPageButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

You can Simply Override The Default Background With Your Color您可以简单地用您的颜色覆盖默认背景

<Button
    android:id="@+id/cameraPageButton"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:text="Camera Page Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:background="@color/colorAccent"
    app:layout_constraintStart_toStartOf="parent"/>

I don't think you are able to modify this behavior in XML.我认为您无法在 XML 中修改此行为。 In my personal opinion, you can use a TextView to achieve the same effect.在我个人看来,你可以使用一个 TextView 来达到同样的效果。 If you need a similar background, (like the default Button itself) you can create a drawable with rounded corners, and if you need a slight shadow too you can add elevation.如果您需要类似的背景(如默认的 Button 本身),您可以创建一个带有圆角的 drawable,如果您也需要一个轻微的阴影,您可以添加高度。

    <TextView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#fff"
        android:fontFamily="@font/m_bo"
        android:gravity="center"
        android:text="press"
        android:textAllCaps="true"
        android:textColor="#242424"
        android:textSize="@dimen/text_size_large" />

With the Support Library v7, all the styles are actually already defined and ready to use.使用支持库 v7,所有样式实际上都已经定义并可以使用。

So you can set your button style like this and it will fill all the space所以你可以像这样设置你的按钮样式,它会填满所有的空间

<Button
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="BUTTON"
    android:gravity="center"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:background="@color/colorAccent"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM