【问题标题】:TextView: Get x and y for the end of the last lineTextView:获取最后一行末尾的 x 和 y
【发布时间】:2019-07-06 17:49:25
【问题描述】:

我有一个TextView,它有多行文本,我想在最后一行文本的末尾显示一个图标。我尝试了几种方法来获取我的 textview 的 x 和 y 值:

      myTextView.viewTreeObserver.addOnGlobalLayoutListener {
      val layout = myTextView.layout
      val lines = myTextView.lineCount
      val myImage = ImageView(activity)
      myImage.imageResource = R.drawable.ic_icon

      // a few ways I tried
      val possibleY = layout.getLineTop(lines).toFloat()
      streamTitleView.measure(0, 0)
      val possibleY2= myTextView.top.toFloat()
      // same for x value

      myImage.x= //x end of last line of text
      myImage.y= //y end of last line of text

      content.addView(myImage)
    }

这并没有给我正确的值。那么有什么方法可以为TextView 中最后一行的文本获取 x 和 y,以便我可以使用这些值为我的 imageview 设置 x 和 y?

【问题讨论】:

    标签: android layout textview


    【解决方案1】:

    您是否曾尝试使用image.getX()image.getY() 来获取它?也许这可以帮助您获得X和Y坐标的位置

    【讨论】:

    • 没有。我不需要 imageview 的坐标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2018-05-01
    • 2017-03-01
    相关资源
    最近更新 更多