Android 无法更改按钮的背景色

Android 无法更改按钮的背景色,android,android-button,androidx,Android,Android Button,Androidx,我无法通过使用backgroundTint更改android应用程序中任何按钮的背景色,但如果我将色调设置为透明,则按钮上不会出现图标。代码如下: build.gradle: apply plugin: 'com.android.application' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' android { compileSdkVersion 28 buildTool

我无法通过使用
backgroundTint
更改android应用程序中任何
按钮的背景色,但如果我将色调设置为透明,则按钮上不会出现图标。代码如下:

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.abc"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 121
        versionName "2.2.95"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.google.guava:guava:16.0.1'
    implementation 'androidx.annotation:annotation:1.0.0-beta01'
    implementation 'org.apache.commons:commons-io:1.3.2'

}
repositories {
    mavenCentral()
}
Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context="com.abc.act">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:gravity="center"
            android:text="@string/app_name"
            android:textStyle="bold"
            android:textColor="#a8c476"
            android:textSize="32sp" />

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="35dip"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:background="@drawable/custom_spinner">

                <Spinner
                    android:id="@+id/abc4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="true"
                    android:entries="@array/protocolItems"
                    android:prompt="@string/protocol_default" />
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/abc1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8.8"
                android:background="@drawable/custom_spinner"
                android:hint="@string/hint_text"
                android:inputType="text"
                android:maxLines="1"
                android:padding="5dp"
                android:textSize="18sp" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:backgroundTint="@android:color/white"
                android:padding="5dp"

                app:icon="@android:drawable/ic_dialog_info" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="35dip"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:background="@drawable/custom_spinner">

                <Spinner
                    android:id="@+id/abc2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="true"
                    android:entries="@array/spinnerItems"
                    android:prompt="@string/spinner_default" />
            </FrameLayout>
        </LinearLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingBottom="3dp">

            <Button
                android:id="@+id/abc3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enter"
                android:textColor="#ffffff"
                android:textSize="14sp" />
        </FrameLayout>

    </LinearLayout>
</LinearLayout>

Manifest.xml

<application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar">
</application>


我已经尝试过更改按钮的背景,更改主题的样式。

对于背景颜色,请使用:

android:background="@android:color/white"
而不是:

android:backgroundTint="@android:color/white"
对于图标,请使用:

android:drawableLeft="@drawable/ic_dialog_info"
而不是:

app:icon="@android:drawable/ic_dialog_info"

对于背景色,请使用:

android:background="@android:color/white"
而不是:

android:backgroundTint="@android:color/white"
对于图标,请使用:

android:drawableLeft="@drawable/ic_dialog_info"
而不是:

app:icon="@android:drawable/ic_dialog_info"
背景色调使整个按钮具有相同的颜色。您应该使用前台属性
android:Foreground=“@color/colorPrimary”

背景色调使整个按钮具有相同的颜色。您应该使用前台属性
android:Foreground=“@color/colorPrimary”


您可以使用如下代码:

<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1.2"
            android:padding="5dp"
            android:drawableLeft="@android:drawable/ic_dialog_info"
            android:background="#000" />
而不是

app:icon=""
backgroundTint

而不是

app:icon=""
backgroundTint

您可以使用如下代码:

<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1.2"
            android:padding="5dp"
            android:drawableLeft="@android:drawable/ic_dialog_info"
            android:background="#000" />
而不是

app:icon=""
backgroundTint

而不是

app:icon=""
backgroundTint

您可以使用此按钮为按钮添加颜色和图标 ```


您不能在FrameLayout中使用android:layout\u weight=“1.2”,它是无效的
布局参数。

您可以使用此参数为按钮添加颜色和图标 ```


您不能在FrameLayout中使用android:layout\u weight=“1.2”,它是无效的

布局参数。

你到底想要什么?你想做什么?这真的让人困惑。如果你想改变按钮的背景色,目前默认为黑色。你到底想要什么?你想做什么?这真的让人困惑。想要更改按钮的背景色,目前默认为黑色。它正在更改颜色,但我现在如何设置图标?我尝试了app:icon,但它不工作且无法绘制:left也无法设置图标。它正在更改颜色,但我现在如何设置图标?我尝试了app:icon,但它不起作用,可绘制:left也不起作用,无法设置图标。这不起作用,请参见@Mr.Robot的答案,它更改了颜色,但现在图标未通过使用android:drawableLeft或app:icon进行设置。这不起作用,请参见@Mr.Robot的答案,它更改了颜色,但现在图标未通过使用android:drawableLeft或app:icon进行设置。这不起作用,请参见@Mr.Robot的答案,它更改了颜色,但现在图标未进行设置。使用android:drawableLeft或app:iconI对其进行了测试,结果正常。您选择的图标为灰色,请注意,你的按钮背景不是灰色的,它仍然是黑色的,android:drawableLeft=“”对我的布局没有影响为什么你的按钮宽度是0 dp?将其更改为包装内容,并查看未工作??未工作,宽度为0dp,因为我已为edittextThis not working分配了权重,请参见@Mr.Robot的答案,它更改了颜色,但现在图标未设置,无论是使用android:drawableLeft还是应用程序:iconI对其进行了测试,这可以正常工作您选择的图标是灰色的,请注意,您的按钮的背景不是灰色的。它仍然是黑色的,android:drawableLeft=“”对我的布局没有影响为什么您的按钮宽度是0 dp?将其更改为包装内容,并查看尚未工作??不工作,宽度为0dp,因为我已为EditText指定了权重为什么在FrameLayout中使用android:layout\u权重?你试过移除它吗?为什么你在FrameLayout中使用android:layout\u weight?你试过移除它吗?