This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint less…
項目遷移到AndroidX後,運行項目時發現androidx.constraintlayout.widget.ConstraintLayout佈局中的子控件提示如下錯誤
此時運行項項目時會出現崩潰的現象
此時,你要檢查一下ConstraintLayout中的約束條件是否完整
所謂的完整就是水平和垂直的約束都要有
比如之前的是這麼寫的,這個時候,TextView是有紅線提示錯誤的。
<TextView
android:id="@+id/projectNameTv"
android:layout_width="wrap_content"
android:text="某某設施點"
android:textColor="@color/black"
android:textSize="@dimen/font_16"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"/>
是因爲我只添加了垂直方向的約束
app:layout_constraintTop_toTopOf="parent"
水平方向的約束沒有添加,在AndroidX中就提示錯誤了。
那麼把水平方向的約束也添加上就好了。
如下
<TextView
android:id="@+id/projectNameTv"
android:layout_width="wrap_content"
android:text="某某設施點"
android:textColor="@color/black"
android:textSize="@dimen/font_16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"/>
然後確保你依賴的版本是最新的穩定版,可在Maven中查詢最新的穩定版
androidx.constraintlayout版本查詢
目前最新的穩定版是1.1.3
新的測試版還是暫時不要用了,可能會出現意想不到的bug
如果你覺得本文對你有幫助,麻煩動動手指頂一下,算是對本文的一個認可,如果文中有什麼錯誤的地方,還望指正,轉載請註明轉自喻志強的博客 ,謝謝!