How to group multiple views in a ConstraintLayout

前端 未结 2 1787
滥情空心
滥情空心 2021-02-01 02:52

I have added 3 buttons in a ConstraintLayout. I have added a button to disable or enable these buttons.

If I was using normal LinearLayout. I could have put all the butt

2条回答
  •  攒了一身酷
    2021-02-01 03:43

    Yes, as I know you can handle visibility using linear layout but not Enable/Disable views I think, correct me if I am wrong. So now in ConstraintLayout also we can handle visibility of particular group of views using the Group

    This is new feature introduced in ConstraintLayout which is currently in Beta version.

    How to add beta ConstraintLayout to project follow steps below

    add maven support in project gradle file as below

    allprojects {
        repositories {
            maven { url 'https://maven.google.com' }
            jcenter()
        }
    }
    

    then in app gardle dependencies add ConstarintLayout library dependency

    compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
    

    now you have to add group in your ConstraintLayout as follow

      
    

    where in Group reference id

    app:constraint_referenced_ids="button7,button3,button2"
    

    contains the comma separated view id's you want to handle run time, so in activity you just bind Group as below and handle visibility

    import android.support.constraint.Group; //import statement in activity
    
    Group group=(Group)findViewById(R.id.group);//bind view from xml
    group.setVisibility(View.VISIBLE);//this will visible all views
    group.setVisibility(View.GONE);//this will set Gone to all views
    group.setVisibility(View.INVISIBLE);//this will set INVISIBLE to all view
    

    EDIT ConrtsaintLayout 1.1.0 stable version released on 12 April 2018 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html

    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    Edit Android X If anyone using android x package you can find package info here

    https://developer.android.com/jetpack/androidx/migrate

    and use:

    
    

提交回复
热议问题