有没有办法删除android中按钮的默认样式?

问题描述 投票:0回答:1

我在 android studio xml 文件中有按钮,当我更改它时,它的形状和颜色没有改变 我不想创造自己的风格 看起来怎么样

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.PlantsListFragment">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="#1F1F27">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spaces"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:layout_centerInParent="true"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_square"
            android:backgroundTint="#1C2F33" />

    </RelativeLayout>

</FrameLayout>

如果需要的话,这里还有我的 rounded_square.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#1C2F33" />
    <corners android:radius="30dp" />
</shape>

我希望这个按钮的颜色为“#1C2F33”并且我希望它有圆角

android xml android-layout
1个回答
0
投票

不要使用Button,而是使用AppCompatButton,然后对其应用可绘制的背景形状。您也不需要背景色调,因为颜色已经在可绘制形状中指定。

 <androidx.appcompat.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_square" />

还有更多背景信息。 您不是删除样式,而是替换它。 默认情况下,Android 中的每个控件都继承自其应用程序(您在清单中指定的 launchTheme)或活动的主题(也在清单中指定)传递的主题。

控件可以有自己的样式,您只需创建一个样式然后将其应用到您的控件即可。该主题将自动替换从其父上下文(活动/应用程序)传递下来的主题。

如果您使用 Material 主题,请记住仅使用 AppCompat 控件。 AppCompat 允许更轻松的自定义,而不是基本控件(例如按钮)。

© www.soinside.com 2019 - 2024. All rights reserved.