当前位置:  开发笔记 > Android > 正文

How to achieve the following layout with dynamic width in Constraint Layout?

如何解决《HowtoachievethefollowinglayoutwithdynamicwidthinConstraintLayout?》经验,为你挑选了1个好方法。

I have a constraint layout with a simple card view with a text inside, and a text at the bottom. I want the card's width to be at least the width of the bottom text. So something like setting a minimum width.

I drew a picture illustrating what I want but wasn't able to implement it in Constraint Layout.



1> RobDev..:

Barriers will do the magic for you!
With this feature you automatically get the width of the widest object in a certain scope.

From Android Documentaion:

A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.

The following xml will check the widest width of the views content_text and bottom_text

    

Now you can use this barrier as a constrain in any other view (inside the same ConstraintLayout)

    

That's what my result looks like:

Sample 1

Sample 2

My complete XML:




    

    

    


    
    


Hope this helps.
Here could you read more about barriers

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有