本文介绍了在约束布局中切换链组的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前的xml布局中,我有多个视图组,其中的元素很少.隐藏每个视图组也将隐藏其所有子元素.由于我想采用扁平结构,因此尝试了 ConstraintLayout .酷,我知道如何将元素与传播链正确对齐.由于平面结构没有包装 LinearLayout ,现在我要隐藏3个视图.我想知道是否还有其他方法可以实现这一目标.

In previous xml layout, I have multiple view groups with few elements inside. Hide each view group will also hide all of its child elements. Since I wanted to have flat structure and tried ConstraintLayout. Cool I know how to chain element with spread to align properly. Since flat structure does not have wrapped LinearLayout, now i have 3 views to hide instead. I would like to know if there is alternative to achieve this.

无约束布局

<RelativeLayout....
..........
..........
<LinearLayout
        android:visibility="gone"
        tools:visibility="visible"
        android:id="@+id/filter_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblTerminal"
            android:background="@color/lightGray"
            style="@style/PurpleSubtitle"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            android:padding="10dp"
            android:text="@string/lblTerminal"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <View
            android:background="@android:color/black"
            android:layout_width="1dp"
            android:layout_height="match_parent"/>

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblCategory"
            android:background="@color/lightGray"
            android:padding="10dp"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            style="@style/PurpleSubtitle"
            android:text="@string/lblCategory"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />


    </LinearLayout>
  .......
  .......
  </RelativeLayout>

采用约束布局

    <android.support.constraint.ConstraintLayout
    .....
    .....
    .....
       #happy that i no longer need LinearLayout for align properly
       <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblTerminal"
            android:background="@color/lightGray"
            style="@style/PurpleSubtitle"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            android:padding="10dp"
            android:text="@string/lblTerminal"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintTop_toBottomOf="@+id/txt_search"
            app:layout_constraintRight_toLeftOf="@+id/view3"
            app:layout_constraintLeft_toLeftOf="@+id/guideline2"
            app:layout_constraintHorizontal_chainStyle="spread"/>

        <View
            android:background="@android:color/black"
            android:layout_width="1dp"
            android:layout_height="50dp"
            android:id="@+id/view3"
            app:layout_constraintTop_toBottomOf="@+id/txt_search"
            app:layout_constraintRight_toLeftOf="@+id/lblCategory"
            app:layout_constraintLeft_toRightOf="@+id/lblTerminal" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblCategory"
            android:background="@color/lightGray"
            android:padding="10dp"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            style="@style/PurpleSubtitle"
            android:text="@string/lblCategory"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintTop_toTopOf="@+id/view3"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/view3" />



  ......
  ......
  ......

  </android.support.constraint.ConstraintLayout>

推荐答案

是的,所以现在在 ConstraintLayout 中,我们还可以使用 Group

Yes, so now in ConstraintLayout also we can handle visibility of particular groups of Views using the Group

如何将beta ConstraintLayout添加到您的项目?请按照以下步骤操作:

How to add beta ConstraintLayout to your project? Follow the steps below:

如下所示在项目gradle文件中添加maven支持

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

然后在应用Gradle依赖项中添加ConstraintLayout库依赖项

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

现在,您必须按如下所示在ConstraintLayout中添加Group

Now you have to add a Group in your ConstraintLayout as follows

<android.support.constraint.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="button7,button3,button2"
        android:id="@+id/group" />  

Group中的引用ID ...

Where in Group, the reference id...

app:constraint_referenced_ids="button7,button3,button2"

...包含要处理运行时的逗号分隔视图ID ,因此,在活动"中,您只需按如下所示绑定Group并处理可见性

... contains the comma separated view id's you want to handle run time, so, in an Activity, you just bind the Group as below and handle the 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

ConstraintLayout 1.1.0稳定版于2018年4月12日发布 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html

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

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

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

这篇关于在约束布局中切换链组的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 19:35