Displaying Views Inline

Nathan Sokalski 4,116 Reputation points
2022-11-08T20:53:53.333+00:00

I have a combination of TextView(s) and other View(s) that I want to display inline. In other words, I want the View(s) to wrap to the next line just like the text in a TextView does. In other words, I basically want the View(s) to act as if they were characters in the TextView. Where & what wraps to the next line will obviously depend on the text & screen size, and therefore may be different for different users. Is there a way to do this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,016 Reputation points Microsoft Vendor
    2022-11-09T09:31:25.453+00:00

    Hello,

    Do you want to achieve the result like FlexboxLayout?

    If so, you can search this nugget package management in VS.

    If you want to write it by yourself, you create a custom view that extends ViewGroup. You need to override onMeasure and onLayout method. Because onMeasure and onLayout methods are not achieved in ViewGroup.

    OnMeasure method will measure himself and child Controls’ width and height. All child controls will be measured recursively.
    OnLayout method, you can achieve the inline and newline in OnLayout method, for example, you can get all child controls by View child = GetChildAt(i); and iterate over them. If all of controls width over the measured ViewGroup’s width, we can have a newline.
    For more details, you can refer to this thread.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful