Jetpack Compose - 修饰符顺序 [英] Jetpack Compose - Order of Modifiers

查看:33
本文介绍了Jetpack Compose - 修饰符顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说修饰符是从左边应用的.但从这个例子来看,它们似乎是从右侧应用的:先边框再填充,因为文字和边框之间没有空格

Text(你好!", Modifier.padding(10.dp).border(2.dp, Color.Magenta))

解决方案

  • 在 Android Compose 中,生成的图像是从外层向中心的 Composable 构建的.这意味着首先定义的绿色边框是外边框,最后定义的红色边框是内边框.这非常令人困惑,因为在代码中最接近 Text Composable 的 Green Modifier 在结果中离它最远.
  • 这与 SwiftUI 形成对比,其中修饰符在代码和生成的图像中以相同的顺序出现.在代码中最接近可组合的修饰符在结果图像中也最接近它.
  • 如果您想想象生成的图像是从您的 Composable 所在的中心构建的(如在 SwiftUI 中),则修饰符的应用顺序与它们给出的顺序相反(从底部向上).
  • 因此,如果您拥有带有两个边框修饰符的可组合文本
    • 距离代码中 Text Composable 最远的边框修饰符(底部红色)
    • 将最接近结果图像中的可组合文本
  • 修饰符从外层向内层应用
    • 将 .border(2.dp, Color.Green) 应用到最外层
    • 向内应用 .padding(50.dp)
    • 将 .border(2.dp, Color.Red) 应用到最内层
<块引用>

package com.example.myapplication导入 android.os.Bundle导入 androidx.appcompat.app.AppCompatActivity导入 androidx.compose.foundation.*导入 androidx.compose.foundation.layout.padding导入 androidx.compose.ui.Modifier导入 androidx.compose.ui.graphics.Color导入 androidx.compose.ui.platform.setContent导入 androidx.compose.ui.unit.dp类 MainActivity : AppCompatActivity() {覆盖 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)设置内容{文本(你好!",修饰符.border(2.dp, Color.Green).padding(50.dp).border(2.dp, Color.Red))}}}

Documentation says that Modifiers are applied from the left. But from this example it looks like they are applied from the right: First border and then padding because there is no space between text and border

Text("Hi there!", Modifier.padding(10.dp).border(2.dp, Color.Magenta))

解决方案

  • In Android Compose resulting Image is being constructed from the outside layer toward the Composable in the center. This means that first defined Green border is outer border and the last defined Red border is inner border . This is very confusing since Green Modifier that is closest to Text Composable in the Code is furthest from it in the result.
  • This is in contrast to SwiftUI where Modifiers appear in the same order both in the Code and in the resulting Image. Modifier that is closest to the Composable in the Code is also closest to it in the resulting Image.
  • If you want to imagine that resulting Image is being constructed from the center where your Composable is positioned (like in SwiftUI) then Modifiers are applied in the opposite order from which they are given (from the bottom upward).
  • So if you have Text Composable with two border Modifiers
    • border Modifier that is furthest away from the Text Composable in the Code (the bottom Red one)
    • will be closest to the Text Composable in the resulting Image
  • Modifiers are applied from outer toward inner layer
    • Applying .border(2.dp, Color.Green) to the outmost layer
    • Applying .padding(50.dp) going inward
    • Applying .border(2.dp, Color.Red) to the innermost layer

package com.example.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.unit.dp

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      Text("Hi there!",
        Modifier
          .border(2.dp, Color.Green)
          .padding(50.dp)
          .border(2.dp, Color.Red)
      )
    }
  }
}

这篇关于Jetpack Compose - 修饰符顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆