Android - 修改 Button 的 background 没有效果(原因未知)的解决方法

问题:尝试修改 Layout 文件中 Button 元素的 background 属性,运行应用,在安卓虚拟设备上没有出现预期的效果。

解决方法:修改 src\main\res\values\themes.xml 文件中 style 元素的 parent 属性,将其改为:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- ... 略 -->
    </style>
</resources>

具体原因未知。参考:https://bbs.csdn.net/topics/398221623

猜测:可能和 SDK 版本有关系,app\build.gradle 文件:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

猜你喜欢

转载自blog.csdn.net/qq_29761395/article/details/112198510