在Android手表中的瓷砖中的列布局

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

Column layout in an Tile in Android Watch

问题

I finally got my tile working for my android watch project, but I can't get the layout to work properly. I am trying to have text (dynamically will change based on state and a freshness timer) and a button. I would've thought this would be a very easy thing to do, but for some reason anytime I put content inside of a Column or anything else, nothing shows up in the preview. I don't understand what is going wrong...

class TimerTileRenderer(context: Context): SingleTileLayoutRenderer<TimerTileState, Boolean>(context) {

//... code for produce resources for images and stuff omitted.

override fun renderTile(
state: TimerTileState,
deviceParameters: DeviceParametersBuilders.DeviceParameters
): LayoutElementBuilders.LayoutElement {
return timerTileLayout(
context,
deviceParameters,
state,
emptyClick
)
}
}

private fun timerTileLayout(
context: Context,
deviceParameters: DeviceParametersBuilders.DeviceParameters,
state: TimerTileState,
buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
.setContent(
Column.Builder()
.setHeight(expand())
.setWidth(expand())
.addContent(
Text.Builder().setText("Just a TEST").build()
)
.build()
)
.build()

Using the code above I only see a black screen. I've added a .setPrimaryChipContent call after the setContent call just to make sure it's working and I do see the chip at the bottom of the screen, but no text. I've even tried manually setting the fontStyle of the text to white and still no text.

However, if I remove the Column and just use Text in there, I see the text...

private fun timerTileLayout(
context: Context,
deviceParameters: DeviceParameters,
state: TimerTileState,
buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
.setContent(
Text.Builder().setText("Just a TEST").build()
)
.setPrimaryChipContent(...)
.build()

Using that code my text shows up. My end goal is to have text, then a button below it, but every time I try to use a Column nothing shows up on the screen, just a black screen. Is there some special way to use Column, or Box, or something to get more than one element on the screen?

I know there are things like MultiButtonLayout, but I don't want that style, just some text and a button. Any help is greatly appreciated. Thank you.

英文:

I finally got my tile working for my android watch project, but I can't get the layout to work properly. I am trying to have text (dynamically will change based on state and a freshness timer) and a button. I would've though this would be a very easy thing to do, but for some reason anytime I put content inside of a Column or anything else, nothing shows up in the preview. I don't understand what is going wrong...

class TimerTileRenderer(context: Context): SingleTileLayoutRenderer&lt;TimerTileState, Boolean&gt;(context) {

  //... code for produce resources for images and stuff omitted.

  override fun renderTile(
    state: TimerTileState,
    deviceParameters: DeviceParametersBuilders.DeviceParameters
  ): LayoutElementBuilders.LayoutElement {
    return timerTileLayout(
      context,
      deviceParameters,
      state,
      emptyClick
    )
  }
}

private fun timerTileLayout(
  context: Context,
  deviceParameters: DeviceParametersBuilders.DeviceParameters,
  state: TimerTileState,
  buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
  .setContent(
    Column.Builder()
      .setHeight(expand())
      .setWidth(expand())
      .addContent(
        Text.Builder().setText(&quot;Just a TEST&quot;).build()
      )
      .build()
  )
  .build()

Using the code above I only see a black screen. I've added a .setPrimaryChipContent call after the setContent call just to make sure it's working and I do see the chip at the bottom of the screen, but no text. I've even tried manually setting the fontStyle of the text to white and still no text.

However, if I remove the Column and just use Text in there, I see the text...

private fun timerTileLayout(
  context: Context,
  deviceParameters: DeviceParameters,
  state: TimerTileState,
  buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
  .setContent(
    Text.Builder().setText(&quot;Just a TEST&quot;).build()
  )
  .setPrimaryChipContent(...)
  .build()

Using that code my text shows up. My end goal is to have text, then a button below it, but every time I try to use a Column nothing shows up on the screen, just a black screen. Is there some special way to use Column, or Box, or something to get more than one element on the screen?

I know there are things like MultiButtonLayout, but I don't want that style, just some text and a button. Any help is greatly appreciated. Thank you.

答案1

得分: 2

private fun timerTileLayout(
context: Context,
deviceParameters: DeviceParametersBuilders.DeviceParameters,
state: TimerTileState,
buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
.setContent(
Column.Builder()
.addContent(
Text.Builder().setText("Just a TEST").build()
)
.build()
)
.build()

英文:

OK, so I figured it out, it has something to do with setting the width and height to expand. For whatever reason after I removed those 2 things everything worked perfectly. So, just in case someone else runs into this, here's the solution...

private fun timerTileLayout(
  context: Context,
  deviceParameters: DeviceParametersBuilders.DeviceParameters,
  state: TimerTileState,
  buttonClickable: ModifiersBuilders.Clickable
) = PrimaryLayout.Builder(deviceParameters)
  .setContent(
    Column.Builder()
      .addContent(
        Text.Builder().setText(&quot;Just a TEST&quot;).build()
      )
      .build()
  )
  .build()

huangapple
  • 本文由 发表于 2023年6月8日 06:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427376.html
匿名

发表评论

匿名网友

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

确定