Понимание modifier.<method> vs Modifer.<method>: объяснение эффектов и поведения (Jetpack Compose для разработчиков AndrAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Понимание modifier.<method> vs Modifer.<method>: объяснение эффектов и поведения (Jetpack Compose для разработчиков Andr

Сообщение Anonymous »


I'm working through Android Developers online tutorial for 1st Android apps.

I have a C coding background, but very limited object-oriented experience.

I am trying to sort out what's going on when I call a method with "modifier." vs "Modifier.". I assume the lowercase version calls the method on the object passed to it, whereas the uppercase version is somehow a new object local to the child that called it as a parameter. The lowercase version seems to affect previous methods applied to that "parent". Here's an example of both, with observed behavior. The only difference between the 2 examples is the case (uppercase vs lowercase) of "m" in that "modifier" call in the first Text() call.

The first example is desired behavior. In the first "Text" call, I use "Modifier"(uppercase). This preserves the centered vertical arrangement of the parent column. ("OneQuadrant" function is called 4 times, image below shows the preview pane)


Изображение


@Composable fun OneQuadrant(title: String, info: String, bColor: Color, modifier: Modifier) { Column ( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, modifier = modifier .fillMaxHeight() .background(bColor) .padding(16.dp) ) { Text( text = title, fontWeight = Bold, modifier = Modifier.padding(bottom=16.dp) ) Text( text = info, textAlign = TextAlign.Justify, ) } } The 2nd example is undesired behavior. The first "Text" call uses "modifier" (lowercase). This evidently overrides the centered vertical arrangement in the parent column.


Изображение


@Composable fun OneQuadrant(title: String, info: String, bColor: Color, modifier: Modifier) { Column ( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, modifier = modifier .fillMaxHeight() .background(bColor) .padding(16.dp) ) { Text( text = title, fontWeight = Bold, modifier = modifier.padding(bottom=16.dp) ) Text( text = info, textAlign = TextAlign.Justify, ) } } Q1: In the 2nd example, even if the lowercase version is acting on the parent, why would that undo the centered vertical arrangement? I see that bottom padding and centered are inconsistent, and probably the last call prevails. However, the Column also calls for padding after the Arrangement.Center statement, and that works OK.

Q2: If it is mapping back to the parent, why isn't the call within "Column" also mapping back to its parent, since it was passed as a parameter?

Q3: What is the right mental model here, to understand what's going on? I think I'm confused in part by what the underlying objects are. The composable functions have names that suggest objects, but they are really functions acting on some other object(s)... true?

I did read up on Kotlin "companion objects", but that didn't really help. I'm guessing this is somehow related to pass-by-reference behavior in Kotlin, but I cannot figure it out. Can you please explain, or refer me to a clear reference? Thank you!

In case it is useful, here is the function that calls OneQuadrant 4 times.

fun DisplayQuadrants(modifier: Modifier = Modifier) { Column (modifier.fillMaxWidth()) { Row (modifier.weight(1f)){ OneQuadrant( title = stringResource(R.string.text_composable), info = stringResource(R.string.displays_text), bColor = Color(0xFFEADDFF), modifier = modifier.weight(1f) ) OneQuadrant( title = stringResource(R.string.image_composable), info = stringResource(R.string.creates_composable), bColor = Color(0xFFD0BCFF), modifier = modifier.weight(1f) ) } Row (modifier.weight(1f)){ OneQuadrant ( title = stringResource(R.string.row_composable), info = stringResource(R.string.a_layout), bColor = Color(0xFFD0BCFF), modifier = modifier.weight(1f) ) OneQuadrant( title = stringResource(R.string.column_composable), info = stringResource(R.string.vertical), bColor = Color(0xFFF6EDFF), modifier = modifier.weight(1f) ) } } } This "DisplayQuadrants" is called in MainActivity as follows:
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { QuadrantsTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { DisplayQuadrants(modifier = Modifier) } } } } }

Источник: https://stackoverflow.com/questions/780 ... ior-explan
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Понимание modifier.<method> vs Modifer.<method>: объяснение эффектов и поведения (Jetpack Compose для разработчиков Andr
    Гость » » в форуме Android
    0 Ответы
    102 Просмотры
    Последнее сообщение Гость
  • Modifier.wrapContentWidth() против Modifier.width(IntrinsicSize.Max) в Android Jetpack Compose
    Anonymous » » в форуме Android
    0 Ответы
    35 Просмотры
    Последнее сообщение Anonymous
  • Modifier.wrapContentWidth() против Modifier.width(IntrinsicSize.Max) в Android Jetpack Compose
    Anonymous » » в форуме Android
    0 Ответы
    22 Просмотры
    Последнее сообщение Anonymous
  • Modifier.wrapContentWidth() против Modifier.width(IntrinsicSize.Max) в Android Jetpack Compose
    Anonymous » » в форуме Android
    0 Ответы
    23 Просмотры
    Последнее сообщение Anonymous
  • Modifier.wrapContentWidth() против Modifier.width(IntrinsicSize.Max) в Android Jetpack Compose
    Anonymous » » в форуме Android
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Android»