Java 如何使用android更改按钮颜色:背景为白色边框?

Java 如何使用android更改按钮颜色:背景为白色边框?,java,android,android-studio,Java,Android,Android Studio,正如您可能已经猜到的,我对编码相当陌生。作为我的第一个项目,我想为Tictatcoe编写代码。我的按钮的默认颜色似乎设置为紫色,我想更改它。问题是使用“android:background”什么都不做。如果我使用“android:backgroundTint”,它会根据我的需要改变颜色,但不会填充空间。谢谢你的帮助,谢谢 <TableLayout android:layout_width="match_parent" android:la

正如您可能已经猜到的,我对编码相当陌生。作为我的第一个项目,我想为Tictatcoe编写代码。我的按钮的默认颜色似乎设置为紫色,我想更改它。问题是使用“android:background”什么都不做。如果我使用“android:backgroundTint”,它会根据我的需要改变颜色,但不会填充空间。谢谢你的帮助,谢谢

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.825"
        android:id="@+id/table">

        <TableRow>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:backgroundTint="@color/black"
                android:text="X"
                android:textSize="100dp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="X"
                android:background="@color/white"
                android:textSize="100dp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="X"
                android:background="@color/white"
                android:textSize="100dp" />

其他行是相同的


默认情况下,按钮是一个组件,因此要更改背景颜色,可以使用属性:android:backgroundTint,要填充空间,可以使用属性:android:insetteftandroid:insetTopandroid:insetroghtandroid:insetBottom全部设置为0dp值。此外,要删除默认的角半径,您必须使用属性app:cornerRadius来设置0dp

下面是具有上述属性的示例按钮:

<Button
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:text="X"
   android:textSize="100sp"
   android:backgroundTint="@color/black"
   android:insetLeft="0dp"
   android:insetTop="0dp"
   android:insetRight="0dp"
   android:insetBottom="0dp"
   app:cornerRadius="0dp" />

水平应用其中三个不同颜色和相等空间后的结果如下所示:


背景色调应该适合您。我想知道你所说的不占空间是什么意思。您介意分享屏幕截图吗?因为您使用的是表格布局,您可以将按钮的背景色调设置为@android:color/transparent,然后将表格布局的背景设置为自定义颜色,但为什么我不能单独设置颜色?谢谢您的帮助