Android Jetpack Compose Material 3 无法对齐复选框文本。

huangapple go评论50阅读模式
英文:

Android jetpack compose Material 3 Unable to align checkbox text

问题

  1. 如何使文本“Check me”与复选框对齐?
    您可以尝试以下修改您的代码来使文本与复选框对齐:
Checkbox(
    checked = true,
    onCheckedChange = { },
    modifier = Modifier.alignByBaseline() // 使用此修饰符来使文本与复选框基线对齐
)
Text(
    text = "Check me",
    fontWeight = FontWeight.Bold,
    fontSize = 18.sp,
    textAlign = TextAlign.Left
)
  1. 如何使文本更靠近复选框?
    如果您想使文本更靠近复选框,您可以调整modifier中的absoluteOffset值。负值将使文本向左移动,正值将使文本向右移动。例如:
Checkbox(
    checked = true,
    onCheckedChange = { },
    modifier = Modifier.absoluteOffset((-4).dp, 0.dp) // 调整X轴偏移值
)
Text(
    text = "Check me",
    fontWeight = FontWeight.Bold,
    fontSize = 18.sp,
    textAlign = TextAlign.Left
)

通过这些修改,您可以尝试调整文本与复选框的对齐和间距以获得您想要的效果。

英文:

I am using Android Studio Electric Eel | 2022.1.1.

I am fairly new. I was trying out the checkbox in Compose with Material3 in Android. I am not able to align the text next to check box. Please help. Here is the code and an screenshot image of the emulator screen.

  1. project level build.gradle firl
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
}
  1. module level 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"
}
  1. 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
            )
        }


    }
}



I have attached a screen shot of the emulator screen to show how the text is aligned.

Question:

  1. How can I align the text "Check me" along with check box.
  2. How can I bring it closer to check box.

Please help.

Android Jetpack Compose Material 3 无法对齐复选框文本。

I tried to place text next to checkbox in my android studio material 3 project. The text is not aligned in line with the checkbox.

答案1

得分: 1

以下是翻译好的代码部分:

@Composable
fun Test() {

    var selected by remember { mutableStateOf(false) }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {

        Row {
            Checkbox(checked = selected, onCheckedChange = { selected = it })
            Spacer(modifier = Modifier.width(5.dp))
            Text(
                text = "Check me",
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                color = Color.Black,
                modifier = Modifier.align(CenterVertically)
            )
        }
        Text(
            text = "Right text",
            fontSize = 18.sp,
            color = Color.Blue,
            modifier = Modifier.align(CenterVertically)
        )
    }

}

请注意,我已将代码中的HTML实体“"”翻译为正常的双引号(")以便于理解。

英文:

For that you have to align both the text Center Vertically , Below is the full code.

@Composable
fun Test() {

    var selected by remember { mutableStateOf(false) }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {

        Row {
            Checkbox(checked = selected, onCheckedChange = { selected = it })
            Spacer(modifier = Modifier.width(5.dp))
            Text(
                text = "Check me",
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                color = Color.Black,
                modifier = Modifier.align(CenterVertically)
            )
        }
        Text(
            text = "Right text",
            fontSize = 18.sp,
            color = Color.Blue,
            modifier = Modifier.align(CenterVertically)
        )
    }

}

Android Jetpack Compose Material 3 无法对齐复选框文本。

答案2

得分: 0

您可以使用此指南进行对齐:https://developer.android.com/jetpack/compose/layouts/alignment-lines

我在测试您的代码时,得到了以下结果:

fun Modifier.firstBaselineToTop(
    firstBaselineToTop: Dp
) = layout { measurable, constraints ->
    // 测量组件
    val placeable = measurable.measure(constraints)

    // 检查组件是否具有第一个基线
    check(placeable[FirstBaseline] != AlignmentLine.Unspecified)
    val firstBaseline = placeable[FirstBaseline]

    // 带有填充的组件的高度 - 第一个基线
    val placeableY = firstBaselineToTop.roundToPx() - firstBaseline
    val height = placeable.height + placeableY
    layout(placeable.width, height) {
        // 组件的放置位置
        placeable.placeRelative(0, placeableY)
    }
}

@Composable
fun Test() {
    Row(
        horizontalArrangement = Arrangement.Start,
        modifier = Modifier.padding(0.dp)
    ) {
        Row {
            Checkbox(
                checked = true,
                onCheckedChange = { },
                modifier = Modifier.absoluteOffset((-12).dp, 0.dp)
            )
            Text(
                text = "Check me",
                Modifier.padding(vertical = 10.dp),
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                textAlign = TextAlign.Left
            )
        }
        TextButton(
            onClick = { },
            modifier = Modifier.padding(end = 0.dp)
        ) {
            Text(
                text = "Right text",
                Modifier.firstBaselineToTop(16.dp),
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                textAlign = TextAlign.Right
            )
        }
    }
}
英文:

You can you this guide to align: https://developer.android.com/jetpack/compose/layouts/alignment-lines

I was playing around with your code, and here is what I get:

Android Jetpack Compose Material 3 无法对齐复选框文本。

fun Modifier.firstBaselineToTop(
    firstBaselineToTop: Dp
) = layout { measurable, constraints ->
    // Measure the composable
    val placeable = measurable.measure(constraints)

    // Check the composable has a first baseline
    check(placeable[FirstBaseline] != AlignmentLine.Unspecified)
    val firstBaseline = placeable[FirstBaseline]

    // Height of the composable with padding - first baseline
    val placeableY = firstBaselineToTop.roundToPx() - firstBaseline
    val height = placeable.height + placeableY
    layout(placeable.width, height) {
        // Where the composable gets placed
        placeable.placeRelative(0, placeableY)
    }
}

@Composable
fun Test() {
    Row(
        horizontalArrangement = Arrangement.Start,
        modifier = Modifier.padding(0.dp)
    ) {
        Row {
            Checkbox(
                checked = true,
                onCheckedChange = { },
                modifier = Modifier.absoluteOffset((-12).dp, 0.dp)
            )
            Text(
                text = "Check me",
                Modifier.padding(vertical =10.dp),
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                textAlign = TextAlign.Left
            )
        }
        TextButton(
            onClick = { },
            modifier = Modifier.padding(end = 0.dp)
        ) {
            Text(
                text = "Right text",
                Modifier.firstBaselineToTop(16.dp),
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                textAlign = TextAlign.Right
            )
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月16日 01:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定