美文网首页Android进阶之路
ConstraintLayout 均分

ConstraintLayout 均分

作者: 咚咚_Coding | 来源:发表于2021-01-07 11:16 被阅读0次
image.png

一、Xml这样

  <?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout         xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/xct_space_value_x5"
android:layout_marginTop="@dimen/xct_space_value_x20"
android:layout_marginRight="@dimen/xct_space_value_x5">

<TextView
    android:id="@+id/TextView1"
    style="@style/text_no_top_style"
    android:layout_width="0dp"
    android:layout_height="@dimen/xct_space_value_x100"
    android:layout_marginRight="6dp"
    android:background="@color/xct_lthj_color_dbp_yellow"
    android:text="packed"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/TextView2"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/TextView2"
    style="@style/text_no_top_style"
    android:layout_width="0dp"
    android:layout_height="@dimen/xct_space_value_x100"
    android:layout_marginRight="6dp"
    android:background="@color/colorPrimary"
    android:text="听你"
    android:visibility="gone"
    app:layout_constraintLeft_toRightOf="@id/TextView1"
    app:layout_constraintRight_toLeftOf="@id/TextView3"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/TextView3"
    style="@style/text_no_top_style"
    android:layout_width="0dp"
    android:layout_height="@dimen/xct_space_value_x100"
    android:background="@color/colorRed"
    android:text="听你"
    android:visibility="gone"
    app:layout_constraintLeft_toRightOf="@id/TextView2"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

二、代码只需要一行

        class LineAverageView @JvmOverloads constructor(context:   Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {
init {
      LayoutInflater.from(context).inflate(R.layout.item_layout_space_view, this)
      注意:当data.size=1时 调用下边这一行          TextView1.layoutParams.width=ScreenUtils.getScreenWidth(context)/3-ScreenUtils.dip2px(context,10f);
}
}

相关文章

网友评论

    本文标题:ConstraintLayout 均分

    本文链接:https://www.haomeiwen.com/subject/mzxzoktx.html