自定义ViewGroup实现宽度自动换行
程序员文章站
2022-06-08 17:34:24
...
自定义ViewGroup实现宽度自动换行
自定义ViewGroup
代码
package com.example.app1;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class LiuViewGroup extends ViewGroup {
int groupWidth,groupHeight;
private static final String TAG = "LiuViewGroup";
public LiuViewGroup(Context context) {
super(context);
}
public LiuViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
//onMesure() ——计算childView的测量值以及模式,以及设置自己的宽和高。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
switch (mode){
case MeasureSpec.EXACTLY:
Log.e(TAG, "onMeasure: " );
groupWidth = size;
break;
case MeasureSpec.AT_MOST:
Log.e(TAG, "onMeasure: " );
groupWidth = 50;
break;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
switch (heightMode){
case MeasureSpec.EXACTLY: //
Log.i(TAG, "onMeasure: ");
groupHeight = heightSize;
break;
case MeasureSpec.AT_MOST:
Log.i(TAG, "onMeasure:111 ");
groupHeight = 50;
break;
}
setMeasuredDimension(groupWidth,groupHeight);
}
//通过getChildCount()获取子view数量,
// getChildAt获取所有子View,
//那个所有view 的宽的数据 相加
//如果小于group的宽 横排
//如果大于group的宽,竖排
// 分别调用layout(int l, int t, int r, int b)确定每个子View的摆放位置。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCountWidth = 0;
int left = 0;
//定义布局的.
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
childCountWidth += layoutParams.width;
Log.i(TAG, "onLayout: "+layoutParams.width); //px
Log.i(TAG, "onLayout: "+layoutParams.height);
//摆放子View 的位置的.
//父布局的宽和多个子布局的宽度想计算
int width = getWidth();
Log.i(TAG, "onLayout: "+width);
if(width > childCountWidth){
//横排
Log.i(TAG, "onLayout: 横排");
childAt.layout(left,0,layoutParams.width+left,layoutParams.height);
left += layoutParams.width;
}else{
//竖排
Log.i(TAG, "onLayout: 竖排 ");
childAt.layout(0,layoutParams.height,layoutParams.width,layoutParams.height*2);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
//实现一个布局的属性
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LinearLayout.LayoutParams(getContext(),attrs);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<com.example.app1.LiuViewGroup
android:id="@+id/group"
android:layout_width="250dp"
android:background="@color/colorAccent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:text="哈哈"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp"></TextView>
<TextView
android:layout_width="100dp"
android:text="嘿嘿"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp"></TextView>
</com.example.app1.LiuViewGroup>
</LinearLayout>
</LinearLayout>
上一篇: winform窗体抖动效果
推荐阅读
-
如何设置网页中的一段文字,使它的宽度是页面宽度的62%且能自动换行?_html/css_WEB-ITnose
-
ext.net2.0 GridPanel 实现自动换行
-
ios实现自动获取label高度、宽度及最后一个位置详解
-
Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例
-
Android编程实现自定义Dialog的大小自动控制方法示例
-
ios实现自动获取label高度、宽度及最后一个位置详解
-
Android编程实现自定义Dialog的大小自动控制方法示例
-
Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例
-
Jlabel实现内容自动换行简单实例
-
Android自定义ViewGroup实现堆叠头像的点赞Layout