Jetpack Compose Material3大/中型TopAppBar容器颜色异常问题
问题:Material3 Large/Medium TopAppBar容器颜色仅作用于图标区域,标题区域不生效
我在使用Material3的Large和Medium TopAppBar时,设置containerColor后,颜色只应用到了导航图标和操作图标所在区域,标题部分没有变色,但Small和Center TopAppBar不存在这个问题。想确认是使用错误还是设计如此?
相关代码
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { YourProjectNameTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Column(modifier = Modifier.fillMaxSize()) { MyUI() } } } } } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun MyUI() { val contextForToast = LocalContext.current.applicationContext LargeTopAppBar( title = { Text(text = "SemicolonSpace") }, navigationIcon = { IconButton(onClick = { Toast.makeText(contextForToast, "Nav Icon Click", Toast.LENGTH_SHORT).show() }) { Icon(imageVector = Icons.Filled.Menu, contentDescription = "Menu") } }, actions = { IconButton(onClick = { Toast.makeText(contextForToast, "Add Click", Toast.LENGTH_SHORT).show() }) { Icon(imageVector = Icons.Default.Add, contentDescription = "Add Items") } }, colors = TopAppBarDefaults.largeTopAppBarColors( containerColor = Color.Green.copy(alpha = 0.3f) ) ) }
运行效果

使用的Gradle配置
项目级Gradle文件
plugins { id 'com.android.application' version '8.0.1' apply false id 'com.android.library' version '8.0.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false }
应用级Gradle文件
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.example.yourprojectname' compileSdk 33 defaultConfig { applicationId "com.example.yourprojectname" minSdk 24 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.3.2' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.5.1' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }
修复方案
目前该问题已在Google官方问题追踪平台上报,暂无官方修复方案。尝试过更换版本、使用background()修饰符均无效,当前可行的解决办法是用Surface或Box组件自定义顶部导航栏。
内容的提问来源于stack exchange,提问作者Kumar
相关产品推荐
相关产品推荐

