kotlin 为什么我在theme.kt中定义的Color主题没有显示在我的物理设备中(调试)

c6ubokkw  于 2022-11-30  发布在  Kotlin
关注(0)|答案(2)|浏览(116)

我已经将应用的主题定义为动态颜色(Material You)。但是每当我在支持Material You的物理设备上运行应用,并且从设置中打开它时,它就不会遵循主题。所有元素都是紫色的,即使在更换墙纸时也是如此。此外,在Dark模式下,它也会显示白色背景和相同的紫色。
在演示功能,我有一个填充色调图标按钮和下拉菜单项containg 5菜单项与4-5字符长的文本。
我在Gruda Linux中使用Android Studio Dolphine。两者都已更新。
我的依赖项版本(& V):

  • build.gradle(项目)
uildscript {
    ext {
        compose_version = '1.3.1'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
  • 构建.gradle(模块)
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.f.rateme'
    compileSdk 33

    defaultConfig {
        applicationId "com.f.rateme"
        minSdk 31
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}


dependencies {

    def nav_version = "2.5.3"

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.compose.material3:material3:1.0.1'
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
    testImplementation 'junit:junit:4.13.2'
    implementation("androidx.navigation:navigation-compose:$nav_version")
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

}

我的主题.kt在ui.主题中

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext


@Composable
fun AppTheme(
  useDarkTheme: Boolean = isSystemInDarkTheme(),
  content: @Composable() () -> Unit
) {

    val colors = if (useDarkTheme) dynamicDarkColorScheme(LocalContext.current)
                 else dynamicLightColorScheme(LocalContext.current)

    MaterialTheme(
    colorScheme = colors,
    typography = typography,
    content = content
    // shapes = shapes, TODO - Maybe shapes parameter required for Material3 theme?
  )
}

我的主要活动.kt

package com.<company_name>.<app_name>

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

import com.<company_name>.<app_name>.ui.theme.AppTheme


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                    var paged = ""

                    paged = feed("Feed")

// PAGE VIEWS

// Feed View
@Composable
fun feed(page: String): String {

    var next = page

    var new_post by remember { mutableStateOf(false) }

    Row() {
        FilledTonalIconButton(
            onClick = { new_post = true },
            enabled = true
        ) {
            Icon(
                Icons.Rounded.Add,
                "Button for creating a new post"
            )
        }

        DropdownMenu(
            expanded = new_post,
            onDismissRequest = { new_post = false }
        ) {
            DropdownMenuItem(text = { Text("Text") }, onClick = { next = "New - Text" }, enabled = true, leadingIcon = { /*TODO*/ })

            DropdownMenuItem(text = { Text("Image") }, onClick = { next = "New - Image" }, enabled = true, leadingIcon = { /*TODO*/ })

            DropdownMenuItem(text = { Text("Video") }, onClick = { next = "New - Video" }, enabled = true, leadingIcon = { /*TODO*/ })

            DropdownMenuItem(text = { Text("Audio") }, onClick = { next = "New - Audio" }, enabled = true, leadingIcon = { /*TODO*/ })

            DropdownMenuItem(text = { Text("Other") }, onClick = { next = "New - Other" }, enabled = true, leadingIcon = { /*TODO*/ })
        }
    }

       return next

}

我正在使用材料3与材料你在Jetpack组成和使用图标的材料(谷歌fints)
截图:

  • 材料你是打开:

  • 在灯光模式下:

  • 在暗模式下

x1c4d 1x指令集

如果你发现任何错字,请更新它或告诉我修复。
PS:这是我第二次在这里发帖,我对Android开发和Kotlin几乎没有经验。我开发了终端应用程序,用Python,C++,C做了ML工作。所以我可能需要更多的信息来解释。我一周前才开始学习Android开发。
你可以问我更多的信息。
很抱歉很长的职位。我想包括一切加快。
和平🏽

o8x7eapl

o8x7eapl1#

它被自动修复。2天后它突然改变为设备动态颜色。我不知道改变了它,但它不再是问题。所以我关闭这个问题。

相关问题