WrapLayout轻量级换行组件

联合创作 · 2023-09-23 07:26

一款轻轻轻轻量级的自动换行布局,可以设置 Gravity。

懒人使用方法

项目目录——>build.gradle

allprojects {

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

app——>build.gradle
  
  	dependencies {
  
       		...
       
	     implementation 'com.github.pimaryschoolstudent:WrapLayout:1.0.0'
	     
	}

导入成功后像一般的布局使用即可,可以参考项目中的 MainActivity

动态添加组件

var tv = TextView(this)
tv.text = "新添加的TextView"
tv.setBackgroundColor( Color.WHITE)
var lp = ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT)
wrapLayout.addView(tv,lp)
<!--gravity 设置重心-->
<primary.student.wraplayout.WrapLayout
    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="wrap_content"
    app:gravity="center"  
    android:id="@+id/wrapLayout"
    android:background="@color/colorPrimaryDark">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="1"
        android:layout_margin="10dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        />
  
</primary.student.wraplayout.WrapLayout>

扩展用法

复制 WrapLayout.java 到你的项目中,在 res/values/attrs.xml

 <declare-styleable name="WrapLayout">
        <attr name="gravity">
            <enum name="center" value="1001"/>
            <enum name="left" value="1002"/>
            <enum name="right" value="1003"/>
        </attr>
</declare-styleable>

然后就可以自由扩展了

浏览 2
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报