代码之家  ›  专栏  ›  技术社区  ›  Nikolay Kulachenko

在ConstraintLayout xml中取消设置属性

  •  1
  • Nikolay Kulachenko  · 技术社区  · 7 年前

    我有一个默认样式 ConstraintLayout 行为现在我想重置 layout_constraintTop_toTopOf 属性以获取正确的约束 layout_constraintTop_toBottomOf 。如何删除 layout\u constraintTop\u toTopOf布局 从视图中?

    样式。xml:

    <style name="ConstraintDefault">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
        <item name="layout_constraintBottom_toBottomOf">parent</item>
        <item name="layout_constraintEnd_toEndOf">parent</item>
        <item name="layout_constraintStart_toStartOf">parent</item>
        <item name="layout_constraintTop_toTopOf">parent</item>
    </style>
    

    main\u布局。xml:

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include
            android:id="@+id/include_toolbar"
            layout="@layout/layout_toolbar" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvDummy"
            style="@style/ConstraintDefault"
            app:layout_constraintTop_toBottomOf="@id/include_toolbar" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            style="@style/Fab"
            app:srcCompat="@drawable/ic_add" />
    
    </android.support.constraint.ConstraintLayout>
    

    结果: RecyclerView 重叠 Toolbar 因为 app:layout_constraintTop_toTopOf="parent"

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  1
  •   Jeff Padgett    4 年前

    unset 样式中的约束集,请使用:

    app:layout_constraintTop_toTopOf="@null"

        2
  •  0
  •   Tenzin Choklang    7 年前

    对于像我认为约束布局是一种过分的做法,您只有一个recylcer视图,我建议您使用协调器布局。 像这样的

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="Your Context"
        tools:showIn="@layout/activity_main">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout> 
    
        3
  •  0
  •   Nikolay Kulachenko    4 年前

    要取消设置约束布局属性,必须使用负数对其进行阴影处理:

    app:layout_constraintTop_toTopOf="-1"   // Upd: It's DEPRECATED! Use the method below
    

    结果发现搬家不是个好主意 布局 属性到 style.xml 。写起来好多了 风格 此处的属性( background ,则, textSize ,则, font ,等等)

    使现代化

    对于较新的约束布局版本,此方法不再有效,并引发编译时错误。正如jeff padgett所建议的,您应该使用空引用:

    app:layout_constraintTop_toTopOf="@null"