Tix*_*eon 19 xml android visibility android-layout android-constraintlayout
在以前的xml布局中,我有多个视图组,其中包含很少的元素.隐藏每个视图组也会隐藏其所有子元素.因为我想要平面结构并尝试过ConstraintLayout.酷我知道如何链接元素与蔓延正确对齐.由于扁平结构没有包裹LinearLayout,现在我有3个视图隐藏.我想知道是否有替代方案来实现这一目标.
没有约束布局
<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>
Run Code Online (Sandbox Code Playgroud)
约束布局
<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>
Run Code Online (Sandbox Code Playgroud)
Pav*_*van 40
是的,所以现在在ConstraintLayout中我们也可以使用Group处理特定视图组的可见性
这是ConstraintLayout中引入的新功能,目前处于Beta版本.
如何将beta ConstraintLayout添加到项目中,请按照以下步骤操作
在项目gradle文件中添加maven支持,如下所示
allprojects {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
}
Run Code Online (Sandbox Code Playgroud)
然后在app gardle依赖项中添加ConstarintLayout库依赖项
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
Run Code Online (Sandbox Code Playgroud)
现在您必须在ConstraintLayou中添加组,如下所示
<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" />
Run Code Online (Sandbox Code Playgroud)
在组参考ID中的位置
app:constraint_referenced_ids="button7,button3,button2"
Run Code Online (Sandbox Code Playgroud)
包含要处理运行时的逗号分隔视图ID,因此在活动中,您只需按如下方式绑定Group并处理可见性
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
Run Code Online (Sandbox Code Playgroud)
编辑ConrtsaintLayout 1.1.0稳定版本于2018年4月12日发布 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
Run Code Online (Sandbox Code Playgroud)
如果您使用的是约束布局的测试版,请遵循@pavan 的回答
如果您使用的是 AndroidX,请按照以下步骤集成约束布局和组:
1)在你的项目中添加 AndroidX 约束布局依赖:
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Run Code Online (Sandbox Code Playgroud)
2)在您的项目中使用如下所示的 ConstraintLayout Group:
<androidx.constraintlayout.widget.Group
android:id="@+id/groupDetails"
android:layout_width="wrap_content"
android:visibility="gone" // Default visibility for group views
app:constraint_referenced_ids="textViewUserName, ..." // id's which you want to include in group
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
3)这是切换可见性的编码部分:
private lateinit var groupDetails:Group
...
groupDetails = findViewById(R.id.groupDetails)
groupDetails.visibility = View.GONE // Change visibility
Run Code Online (Sandbox Code Playgroud)
希望它在使用 AndroidX 时有所帮助。
| 归档时间: |
|
| 查看次数: |
11250 次 |
| 最近记录: |