Skip to content

androidx kotlin dsl 方式编写代码布局(Kotlin dsl Write code layout)

Notifications You must be signed in to change notification settings

BugRui/kotlinCodeLayout

Repository files navigation

kotlinCodeLayout version

androidx kotlin dsl 方式编写代码布局(Kotlin dsl Write code layout)

Step 1. Add the JitPack repository to your build file

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

  implementation 'com.github.BugRui:kotlinCodeLayout:1.0.0-alpha'

因为kotlinCodeLayout只在内部使用compileOnly,并且依赖于AppCompat和ConstraintLayout,所以需要将以下依赖项添加到自己的项目中,否则compileOnly将无法通过打包

Because kotlinCodeLayout USES compileOnly internally and relies on AppCompat and ConstraintLayout, you need to add the following dependencies to your own project, otherwise the compileOnly will not pass

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta7'  //constraintlayout 需要2.0以上 (More than 2.0)

Step 3. Java1.8


compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

使用 (use)

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

kotlinCodeLayout


_ConstraintLayout(context).apply {
	setParams(width = layoutMatchParent, height = layoutMatchParent)
	TextView(context).apply{
	   text="Hello World!"	
	}.lparams(width = layoutWrapContent, height = layoutWrapContent) {
          startToStart = PARENT_ID
          endToEnd = PARENT_ID
          topToTop = PARENT_ID
          bottomToBottom = PARENT_ID
        }.also { addView(it) }
}


对应LayoutParams Corresponding LayoutParams

.lparams(width = layoutWrapContent, height = layoutWrapContent) {
          startToStart = PARENT_ID
          endToEnd = PARENT_ID
          topToTop = PARENT_ID
          bottomToBottom = PARENT_ID
        }

将创建的view添加进父容器 Add the created View to the parent container

.also { addView(it) }

父容器 layout


_ConstraintLayout

_RelativeLayout

_FrameLayout

_GridLayout

_LinearLayout

_ScrollView

_HorizontalScrollView

_Toolbar

_GridView

_RadioGroup

_TableLayout

_TableRow

_ImageSwitcher

_AppWidgetHostView

_ViewAnimator

公共 common


val layoutMatchParent: Int = android.view.ViewGroup.LayoutParams.MATCH_PARENT

val layoutWrapContent: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT


About

androidx kotlin dsl 方式编写代码布局(Kotlin dsl Write code layout)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages