简体   繁体   English

按钮与 Jetpack Compose 中的视图重叠

[英]Button overlap with view in jetpack compose

I want to make a button to bottom of screen, but I am seeing overlap with my button.我想在屏幕底部制作一个按钮,但我看到与我的按钮重叠。 I don't understand why this is happening in my application.我不明白为什么在我的应用程序中会发生这种情况。

PairContentStateLess PairContentStateLess

@Composable
fun PairContentStateLess(
    viewModel: XyzViewModel,
    scanning: State<Boolean>,
    onResume: () -> Unit,
    onPause: () -> Unit,
    tryAgainAction: () -> Unit,
    openSettingAction: () -> Unit,
    onResumeScan: () -> Unit,
    onPauseScan: () -> Unit,
) {
    AnimatedVisibility(visible = true) {
        AppBarScaffold() {
            Column(
                modifier = Modifier
                    .padding(10.dp)
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState()),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = if (viewModel.isBluetoothEnabled) {
                    Arrangement.Top
                } else {
                    Arrangement.SpaceBetween
                }
            ) {
                DisposeOnLifecycleManager(
                    onCreate = {
                        onResumeScan()
                    },
                    onResume = {
                        onResume()
                    },
                    onPause = {
                        onPause()
                        onPauseScan()
                    }
                )
                PairScreenImage()
                PairDeviceDescription()
                if (viewModel.isBluetoothEnabled) {
                    PressAndHoldDescription()
                    WaitingToPair(scanning.value)
                    UnableToPair(scanning.value)
                } else {
                    BluetoothTurnOnWarning()
                    Spacer(modifier = Modifier.weight(1f))
                    TryAgainButtonView { tryAgainAction() }
                    OpenDeviceSettingsButtonView { openSettingAction() }
                }
            }
        }
    }
}

I divided all views into simple function. I am adding simple code in each function.我将所有视图划分为简单的 function。我在每个 function 中添加简单代码。

PairScreenImage配对屏幕图像

@Composable
fun PairScreenImage() {
    // image only
}

PairDeviceDescription配对设备描述

@Composable
fun PairDeviceDescription() {
    // text only
}

PressAndHoldDescription长按说明

@Composable
fun PressAndHoldDescription() {
   // text only
}

WaitingToPair等待配对

@Composable
fun ColumnScope.WaitingToPair(scanning: Boolean) {
    Spacer(modifier = Modifier.height(10.dp))
    AnimatedVisibility(scanning) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(OffWhite),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
           // more items here 
        }
    }
}

UnableToPair无法配对

I am adding this function code which is overlap through my view.我正在添加这个 function 代码,它在我看来是重叠的。 Can anyone know why TryAgainButtonView view overlap with WarningBoxView() ?谁能知道为什么TryAgainButtonView视图与WarningBoxView()重叠?

@Composable
fun ColumnScope.UnableToPair(scanning: Boolean) {
    AnimatedVisibility(!scanning) {
        WarningBoxView()
        TryAgainButtonView {

        }
    }
}

View overlap look like this查看重叠看起来像这样

在此处输入图像描述

I want TryAgainButtonView to bottom of screen.我想要TryAgainButtonView到屏幕底部。

My Component tree from Layout inspector布局检查器中的我的组件树

在此处输入图像描述

Anyone guide me what I am doing here wrong?有人指导我在这里做错了什么吗? Thanks谢谢

I believe this is because you are using Column and not correctly fitting everything on the screen.我相信这是因为您使用的是 Column 而不是正确地适合屏幕上的所有内容。 You hide the image and description composables and it is ok but I think you are giving static dp values in these composables.您隐藏了图像和描述可组合项,这没问题,但我认为您在这些可组合项中提供了 static dp 值。 Rather than giving a size with either .width , .而不是给一个尺寸.width , . height or .size modifiers, use.padding and / or calculate width|height of the composables by using LocalConfiguration.current to properly size the composables. height.size修饰符,使用 .padding 和/或通过使用LocalConfiguration.current来计算可组合项的宽度|高度以正确调整可组合项的大小。 I believe you will see better results.我相信你会看到更好的结果。

Additionally, your Column takes space of the whole screen and tries to fit every child on the possible available space.此外,您的 Column 会占据整个屏幕的空间,并尝试让每个孩子都适应可能的可用空间。 If you are ok with a scrolling screen, use LazyColumn.如果您对滚动屏幕没问题,请使用 LazyColumn。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM