How to use new features in constraint layout 1.1?

前端 未结 4 607
忘掉有多难
忘掉有多难 2020-12-28 15:56

Does anyone know how to use new features in constraint layout 1.1, namely barriers and percent-based dimensions? There is absolutely no documentation available online, and t

4条回答
  •  情话喂你
    2020-12-28 16:38

    1. Percent Dimensions

    The default behavior of widgets of width 0dp (or match_constraint) is spread (configurable through the layout_constraintWidth_default property). In ConstraintLayout 1.0.x, we had a choice to change it to wrap, and in 1.1.x, we have a new value, percent, that allow us to set a widget to take some percentage of the available space.

        
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.4"
    

    2. Barriers

    From this new widget, we have some example from ConstraintLayout.com. Barriers will avoid one or more widgets to bypass the Barrier. When this occurs, the Barrier will move itself, and avoiding the widget(s) to be placed above it. In the example below, the end property of both text1 and text2 cannot bypass the Barrier. When this occurs, the Barrier will move itself to the right (or to the left, if in a RTL layout). This is particulary handful when dealing with different widget sizes, depending of some configuration or internationalization.

    
      
      
      
        app:constraint_referenced_ids="text1,text2" />
      
    
    

    3. Group

    Groups, like the Guidelines, are widgets with size 0. But Group helps to apply some action to a set of widgets. The most common case, is to control a visibility of a collection of widgets. When dealing with this scenario, the most common solution was to maintain yourself a list or set of views inside the Activity or Fragment, or even adding a ViewGroup and put all the views inside of it, controlling the visibility of the container. Now, you only need to add their ids to the Group, and group will propagate the actions to all plugged views.

    
      
      
      
    
    

    In this case, if we call

    group.setVisibility(View.GONE);
    

    then text1 and text2 will receive the visibility GONE.

    Original text here.

    Official docs with description here.

提交回复
热议问题