Android Jetpack Compose Material3 Checkbox文本对齐与间距调整问题
Jetpack Compose Material3 Checkbox 文本对齐与间距调整问题
问题描述
使用Android Studio Electric Eel | 2022.1.1版本,Jetpack Compose结合Material3开发时,遇到两个问题:
- Checkbox旁的“Check me”文本向上偏移,无法与Checkbox对齐
- 希望拉近文本与Checkbox之间的间距
项目代码
项目级build.gradle
buildscript { ext { compose_version = '1.2.0' } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.0' apply false id 'com.android.library' version '7.4.0' apply false id 'org.jetbrains.kotlin.android' version '1.7.0' apply false }
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.example.test' compileSdk 33 defaultConfig { applicationId "com.example.test" minSdk 27 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.2.0' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.compose.material3:material3:1.0.0-alpha11' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 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" }
MainActivity.kt
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { TestTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Test() } } } } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun Test() { Row( horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.padding(0.dp) ) { Row( modifier = Modifier.padding(3.dp) ) { Checkbox( checked = true, onCheckedChange = { }, modifier = Modifier.absoluteOffset((-12).dp, 0.dp) ) Text( text = "Check me", fontWeight = FontWeight.Bold, fontSize = 18.sp, textAlign = TextAlign.Left ) } TextButton( onClick = { }, modifier = Modifier.padding(end = 0.dp) ) { Text( text = "Right text", fontWeight = FontWeight.Bold, fontSize = 18.sp, textAlign = TextAlign.Right ) } } }
解决方案
1. 文本与Checkbox对齐
问题根源是给Checkbox添加了absoluteOffset((-12).dp, 0.dp),破坏了默认布局对齐逻辑。只需给包裹Checkbox和Text的内层Row添加verticalAlignment = Alignment.CenterVertically,让两个组件垂直居中,同时移除Checkbox的absoluteOffset修饰符即可。
修改后的代码片段:
Row( modifier = Modifier.padding(3.dp), verticalAlignment = Alignment.CenterVertically ) { Checkbox( checked = true, onCheckedChange = { } ) Text( text = "Check me", fontWeight = FontWeight.Bold, fontSize = 18.sp, textAlign = TextAlign.Left ) }
2. 拉近文本与Checkbox的间距
Checkbox默认带有右侧内边距,可通过两种方式调整间距:
- 方式一:给Text添加左内边距(推荐,不影响Checkbox点击区域)
Text( text = "Check me", fontWeight = FontWeight.Bold, fontSize = 18.sp, textAlign = TextAlign.Left, modifier = Modifier.padding(start = 4.dp) // 调整数值控制间距大小 )
- 方式二:调整Checkbox的右侧内边距
Checkbox( checked = true, onCheckedChange = { }, modifier = Modifier.padding(end = -4.dp) // 负数值缩小间距 )
内容的提问来源于stack exchange,提问作者Nazir
相关产品推荐
相关产品推荐

