ConstraintLayout中的chainstyle & guideline >

本文深入讲解了ConstraintLayout中的chainstyle和guideline特性。chainstyle包括spread、spread_inside和packed三种模式,通过weight分配空间,利用bias调整位置。guideline则作为辅助线,支持水平和垂直方向,可通过begin、end和percent定位。

ConstraintLayout中的chainstyle & guideline

有时候容易忘记,摘抄记录一下。

参考博客:https://blog.youkuaiyun.com/cui130/article/details/89849807

1、chainstyle(链式)

**属性 **有水平和垂直之分:

  • layout_constraintHorizontal_chainStyle 水平方向
  • layout_constraintVertical_chainStyle 垂直方向

属性值 有三种:

  • spread (默认) 如下:

    • 默认模式如下:

    • 使用weight方式如下:(layout_width=0dp)

      添加属性layout_constraintHorizontal_weight ,同理也有垂直方向的,可以自己尝试

  • spread_inside

  • packed

    • 捆绑在一起居中显示

    • 偏移显示

      添加属性layout_constraintHorizontal_bias

2、guideline(辅助线)

属性 辅助线也有垂直和水平之分,使用属性 orientation 来确定方向

  • horizontal 辅助线为水平方向
  • vertical 辅助线为垂直方向

确定辅助线的位置有三种属性:

  • layout_constraintGuide_begin 水平方向为距离左边的距离;垂直方向为距离上边的距离

    app:layout_constraintGuide_begin="100dp"
  • layout_constraintGuide_end 水平方向为距离右边的距离;垂直方向为距离下边的距离

    app:layout_constraintGuide_percent="0.6"
  • layout_constraintGuide_percent 水平方向为从左向右占比;垂直方向为距离从上到下占比

    app:layout_constraintGuide_end="50dp"

上面举例如下:(垂直方向,水平方向同理)

3、代码如下:
<TextView    
          android:id="@+id/weightStart"    
          android:layout_width="0dp"    
          android:layout_height="30dp"    
          android:text="weightStart"    
          android:textColor="#ffffff"    
          android:background="#83BB96"    
          android:gravity="center"    
          android:layout_marginTop="10dp"    
          app:layout_constraintHorizontal_chainStyle="spread"   
          app:layout_constraintHorizontal_weight="1"    
          app:layout_constraintStart_toStartOf="parent"    
          app:layout_constraintEnd_toStartOf="@id/weightCenter"    
          app:layout_constraintTop_toTopOf="parent"/>

<TextView    
          android:id="@+id/weightCenter"    
          android:layout_width="0dp"    
          android:layout_height="30dp"    
          android:text="weightCenter"    
          android:textColor="#ffffff"    
          android:background="#567B63"    
          android:gravity="center"    
          app:layout_constraintHorizontal_weight="2"   
          app:layout_constraintTop_toTopOf="@+id/weightStart"    
          app:layout_constraintStart_toEndOf="@+id/weightStart"    
          app:layout_constraintEnd_toStartOf="@+id/weightEnd"/>

<TextView    
          android:id="@+id/weightEnd"    
          android:layout_width="0dp"    
          android:layout_height="30dp"    
          android:text="weightEnd"    
          android:textColor="#ffffff"    
          android:background="#669999"    
          android:gravity="center"   
          app:layout_constraintHorizontal_weight="3"   
		  app:layout_constraintTop_toTopOf="@+id/weightStart"    
		  app:layout_constraintStart_toEndOf="@id/weightCenter"    
          app:layout_constraintEnd_toEndOf="parent"/>

<TextView    
          android:id="@+id/insideStart"    
          android:layout_width="wrap_content"    
          android:layout_height="30dp"    
          android:text="insideStart"    
          android:textColor="#ffffff"    
          android:background="#83BB96"    
          android:gravity="center"    
          android:layout_marginTop="10dp"    
          app:layout_constraintHorizontal_chainStyle="spread_inside"   
          app:layout_constraintStart_toStartOf="parent"   
          app:layout_constraintEnd_toStartOf="@id/insideCenter"   
          app:layout_constraintTop_toBottomOf="@+id/weightStart"/>

<TextView    
          android:id="@+id/insideCenter"    
          android:layout_width="wrap_content"    
          android:layout_height="30dp"    
          android:text="insideCenter"   
          android:textColor="#ffffff"    
          android:background="#567B63"   
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/insideStart"   
          app:layout_constraintStart_toEndOf="@+id/insideStart"   
          app:layout_constraintEnd_toStartOf="@+id/insideEnd"/>

<TextView    
          android:id="@+id/insideEnd"   
          android:layout_width="wrap_content"    
          android:layout_height="30dp"   
          android:text="insideEnd"   
          android:textColor="#ffffff"   
          android:background="#669999" 
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/insideStart"   
          app:layout_constraintStart_toEndOf="@id/insideCenter"   
          app:layout_constraintEnd_toEndOf="parent"/>

<TextView   
          android:id="@+id/PackedStart"    
          android:layout_width="wrap_content"   
          android:layout_height="30dp"   
          android:text="PackedStart"  
          android:textColor="#ffffff"   
          android:background="#83BB96"  
          android:gravity="center"  
          android:layout_marginTop="10dp"   
          app:layout_constraintHorizontal_chainStyle="packed"   
          app:layout_constraintStart_toStartOf="parent"   
          app:layout_constraintEnd_toStartOf="@id/PackedCenter"   
          app:layout_constraintTop_toBottomOf="@+id/insideStart"/>

<TextView   
          android:id="@+id/PackedCenter"    
          android:layout_width="wrap_content"    
          android:layout_height="30dp"   
          android:text="PackedCenter" 
          android:textColor="#ffffff"   
          android:background="#567B63"   
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/PackedStart"   
          app:layout_constraintStart_toEndOf="@+id/PackedStart"   
          app:layout_constraintEnd_toStartOf="@+id/PackedEnd"/>

<TextView   
          android:id="@+id/PackedEnd"    
          android:layout_width="wrap_content"  
          android:layout_height="30dp"   
          android:text="PackedEnd"    
          android:textColor="#ffffff"  
          android:background="#669999"  
          android:gravity="center"    
          app:layout_constraintTop_toTopOf="@+id/PackedStart"   
          app:layout_constraintEnd_toEndOf="parent"    
          app:layout_constraintStart_toEndOf="@+id/PackedCenter"/>

<TextView    
          android:id="@+id/PackBiasStart"   
          android:layout_width="wrap_content"  
          android:layout_height="30dp"   
          android:layout_marginTop="8dp"  
          android:background="#83BB96" 
          android:gravity="center" 
          android:text="PackBiasStart"   
          android:textColor="#ffffff"    
          app:layout_constraintHorizontal_chainStyle="packed"   
          app:layout_constraintHorizontal_bias="0.2"    
          app:layout_constraintStart_toStartOf="parent"   
          app:layout_constraintEnd_toStartOf="@id/PackBiasCenter"   
          app:layout_constraintTop_toBottomOf="@+id/PackedStart" />

<TextView   
          android:id="@+id/PackBiasCenter"    
          android:layout_width="wrap_content"  
          android:layout_height="30dp"   
          android:text="PackBiasCenter"  
          android:textColor="#ffffff"   
          android:background="#567B63"  
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/PackBiasStart"    
          app:layout_constraintStart_toEndOf="@+id/PackBiasStart"    
          app:layout_constraintEnd_toStartOf="@+id/PackBiasEnd"/>

<TextView    
          android:id="@+id/PackBiasEnd"    
          android:layout_width="wrap_content"   
          android:layout_height="30dp"   
          android:text="PackBiasEnd"   
          android:textColor="#ffffff"  
          android:background="#669999" 
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/PackBiasStart"   
          app:layout_constraintStart_toEndOf="@id/PackBiasCenter"   
          app:layout_constraintEnd_toEndOf="parent"/>

<TextView   
          android:id="@+id/SpredStart"   
          android:layout_width="wrap_content"  
          android:layout_height="30dp"   
          android:text="SpredStart"  
          android:textColor="#ffffff" 
          android:background="#83BB96"   
          android:gravity="center" 
          android:layout_marginTop="10dp"    
          app:layout_constraintHorizontal_chainStyle="spread"   
          app:layout_constraintStart_toStartOf="parent"   
          app:layout_constraintEnd_toStartOf="@id/SpredCenter"    
          app:layout_constraintTop_toBottomOf="@+id/PackBiasStart"/>

<TextView   
          android:id="@+id/SpredCenter"    
          android:layout_width="wrap_content"  
          android:layout_height="30dp"   
          android:text="SpredCenter"  
          android:textColor="#ffffff"   
          android:background="#567B63"  
          android:gravity="center"    
          app:layout_constraintTop_toTopOf="@+id/SpredStart"    
          app:layout_constraintStart_toEndOf="@+id/SpredStart"    
          app:layout_constraintEnd_toStartOf="@+id/SpredEnd"/>

<TextView 
          android:id="@+id/SpredEnd"   
          android:layout_width="wrap_content"   
          android:layout_height="30dp"  
          android:text="SpredEnd"  
          android:textColor="#ffffff"   
          android:background="#669999" 
          android:gravity="center"   
          app:layout_constraintTop_toTopOf="@+id/SpredStart"   
          app:layout_constraintStart_toEndOf="@id/SpredCenter"   
          app:layout_constraintEnd_toEndOf="parent"/>

<androidx.constraintlayout.widget.Guideline
           android:id="@+id/guideline1"
	   android:layout_width="wrap_content"
	   android:layout_height="wrap_content"
           android:orientation="vertical"
	   app:layout_constraintGuide_begin="100dp"/>

<TextView   
          android:id="@+id/guide1"   
          android:layout_width="wrap_content"    
          android:layout_height="30dp"   
          android:text="SpredEnd"   
          android:textColor="#ffffff" 
          android:background="#669999"  
          android:gravity="center"    
          app:layout_constraintEnd_toStartOf="@+id/guideline1"   
          app:layout_constraintBottom_toBottomOf="parent"/>

<androidx.constraintlayout.widget.Guideline 
		android:id="@+id/guideline2"                                       		   
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"   
 		android:orientation="vertical"  
		app:layout_constraintGuide_end="50dp"/>

<TextView    
          android:id="@+id/guide2"    
          android:layout_width="wrap_content"    
          android:layout_height="30dp"    
          android:text="SpredEnd"  
          android:textColor="#ffffff" 
          android:background="#669999"   
          android:gravity="center"    
          app:layout_constraintEnd_toStartOf="@+id/guideline2"    
          app:layout_constraintBottom_toBottomOf="parent"/>

<androidx.constraintlayout.widget.Guideline
		android:id="@+id/guideline3"    
		android:layout_width="wrap_content"     
		android:layout_height="wrap_content"    
		android:orientation="vertical"   
		app:layout_constraintGuide_percent="0.6"/>

<TextView    
          android:id="@+id/guide3"    
          android:layout_width="wrap_content"   
          android:layout_height="30dp"    
          android:text="SpredEnd"    
          android:textColor="#ffffff"   
          android:background="#669999" 
          android:gravity="center"     
          app:layout_constraintEnd_toStartOf="@+id/guideline3"    
          app:layout_constraintBottom_toBottomOf="parent"/>
我知道哪里有问题了,其实一开始的策略错了,我这个界面应该保持不动的,而item多应该在item那个框里面搞滑动,而不是整个界面无止境的滑动,目前你给的代码如下:&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;androidx.core.widget.NestedScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot; xmlns:tools=&quot;http://schemas.android.com/tools&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:fillViewport=&quot;true&quot; android:fitsSystemWindows=&quot;true&quot; android:layout_marginTop=&quot;?attr/actionBarSize&quot;&gt; &lt;!-- 根布局:ConstraintLayout,match_parent 高度 --&gt; &lt;androidx.constraintlayout.widget.ConstraintLayout android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot;&gt; &lt;!-- 水平指南线 --&gt; &lt;androidx.constraintlayout.widget.Guideline android:id=&quot;@+id/guideline_05&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;horizontal&quot; app:layout_constraintGuide_percent=&quot;0.05&quot; /&gt; &lt;androidx.constraintlayout.widget.Guideline android:id=&quot;@+id/guideline_15&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;horizontal&quot; app:layout_constraintGuide_percent=&quot;0.15&quot; /&gt; &lt;!-- 55% 指南线:地图底部边界(15% + 40% = 55%) --&gt; &lt;androidx.constraintlayout.widget.Guideline android:id=&quot;@+id/guideline_55&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;horizontal&quot; app:layout_constraintGuide_percent=&quot;0.55&quot; /&gt; &lt;!-- 95% 指南线(可选,用于对齐按钮等) --&gt; &lt;androidx.constraintlayout.widget.Guideline android:id=&quot;@+id/guideline_95&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;horizontal&quot; app:layout_constraintGuide_percent=&quot;0.95&quot; /&gt; &lt;!-- 🔍 输入框 --&gt; &lt;EditText android:id=&quot;@+id/search_input&quot; android:layout_width=&quot;0dp&quot; android:layout_height=&quot;wrap_content&quot; android:hint=&quot;请输入需要查询的公交线路或站点&quot; android:textColorHint=&quot;#777777&quot; android:textColor=&quot;@color/black&quot; android:background=&quot;@drawable/rounded_edittext&quot; android:minHeight=&quot;48dp&quot; android:textSize=&quot;16sp&quot; android:padding=&quot;12dp&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toStartOf=&quot;@id/search_btn&quot; app:layout_constraintHorizontal_chainStyle=&quot;packed&quot; app:layout_constraintTop_toTopOf=&quot;@id/guideline_05&quot; app:layout_constraintBottom_toTopOf=&quot;@id/space_after_search&quot; android:layout_marginStart=&quot;16dp&quot; android:layout_marginEnd=&quot;8dp&quot; /&gt; &lt;!-- 🔎 搜索按钮 --&gt; &lt;Button android:id=&quot;@+id/search_btn&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;搜索&quot; app:layout_constraintTop_toTopOf=&quot;@id/guideline_05&quot; app:layout_constraintBottom_toTopOf=&quot;@id/space_after_search&quot; app:layout_constraintStart_toEndOf=&quot;@id/search_input&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:layout_marginEnd=&quot;16dp&quot; /&gt; &lt;!-- ⬛ 搜索栏下方留白(8dp) --&gt; &lt;Space android:id=&quot;@+id/space_after_search&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;8dp&quot; app:layout_constraintTop_toBottomOf=&quot;@id/guideline_15&quot; /&gt; &lt;!-- 🗺️ 地图视图:精确占屏幕 40% 高度 --&gt; &lt;com.amap.api.maps.MapView android:id=&quot;@+id/map_view&quot; android:layout_width=&quot;0dp&quot; android:layout_height=&quot;0dp&quot; app:layout_constraintTop_toBottomOf=&quot;@id/guideline_15&quot; app:layout_constraintBottom_toTopOf=&quot;@id/space_after_map&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt; &lt;!-- ⬛ 地图与结果之间的空白(3% 高度) --&gt; &lt;Space android:id=&quot;@+id/space_after_map&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;0dp&quot; app:layout_constraintTop_toBottomOf=&quot;@id/map_view&quot; app:layout_constraintBottom_toTopOf=&quot;@id/container_result_list&quot; app:layout_constraintHeight_percent=&quot;0.03&quot; /&gt; &lt;!-- 🔲 容器:包裹 RecyclerView 并限制最大高度 --&gt; &lt;androidx.constraintlayout.widget.ConstraintLayout android:id=&quot;@+id/container_result_list&quot; android:layout_width=&quot;0dp&quot; android:layout_height=&quot;0dp&quot; app:layout_constraintTop_toBottomOf=&quot;@id/space_after_map&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; app:layout_constraintBottom_toTopOf=&quot;@id/btn_go_to&quot; app:layout_constraintHeight_max=&quot;300dp&quot;&gt; &lt;!-- 🔽 实际的 RecyclerView --&gt; &lt;androidx.recyclerview.widget.RecyclerView android:id=&quot;@+id/result_list&quot; android:layout_width=&quot;0dp&quot; android:layout_height=&quot;0dp&quot; android:background=&quot;@drawable/rounded_edittext&quot; android:nestedScrollingEnabled=&quot;false&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot; app:layout_constraintBottom_toBottomOf=&quot;parent&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt; &lt;!-- (可选)空状态提示 --&gt; &lt;TextView android:id=&quot;@+id/empty_view&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;暂无搜索结果&quot; android:textColor=&quot;#999999&quot; android:visibility=&quot;gone&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot; app:layout_constraintBottom_toBottomOf=&quot;parent&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt; &lt;!-- &ldquo;到这去&rdquo;按钮 --&gt; &lt;Button android:id=&quot;@+id/btn_go_to&quot; android:layout_width=&quot;0dp&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;到这去&quot; android:layout_margin=&quot;16dp&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; app:layout_constraintBottom_toBottomOf=&quot;parent&quot; app:layout_constraintTop_toBottomOf=&quot;@id/container_result_list&quot; app:layout_constraintVertical_bias=&quot;0&quot; /&gt; &lt;!-- 🌀 加载进度条 --&gt; &lt;ProgressBar android:id=&quot;@+id/progress_bar&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:visibility=&quot;gone&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot; app:layout_constraintBottom_toBottomOf=&quot;parent&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:elevation=&quot;10dp&quot; /&gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt; &lt;/androidx.core.widget.NestedScrollView&gt; 他的缺点是item那个框也无法滑动下去,只能显示这几个结果
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值