Android——自定义LinearLayout自动换行,TextView垂直排列。
来源:互联网 发布:英文seo教程 编辑:程序博客网 时间:2024/06/05 07:28
自定义线性布局在xml中自定换行,比如你在项目中用到LinearLayout,设置水平排列android:orientation=”horizontal”,包裹button或者是TextView,但是不同分辨率的手机,不知道一行能放多少个Button,所以要使用到自动换行的LinearLayout,不多说,贴代码:
package com.ds.platform.view;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.LinearLayout;import java.util.Hashtable;/** * @author: Allen * @date: 2017/3/13 * @description: 自定义linearLayout 自动换行 */public class MyLinearLayout extends LinearLayout { int mLeft, mRight, mTop, mBottom; Hashtable map = new Hashtable(); public MyLinearLayout(Context context) { super(context); } public MyLinearLayout(Context context, int horizontalSpacing, int verticalSpacing) { super(context); } public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int mWidth = MeasureSpec.getSize(widthMeasureSpec); int mCount = getChildCount(); int mX = 0; int mY = 0; mLeft = 0; mRight = 0; mTop = 5; mBottom = 0; int j = 0; View lastview = null; for (int i = 0; i < mCount; i++) { final View child = getChildAt(i); child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); // 此处增加onlayout中的换行判断,用于计算所需的高度 int childw = child.getMeasuredWidth(); int childh = child.getMeasuredHeight(); mX += childw; // 将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置 Position position = new Position(); mLeft = getPosition(i - j, i); mRight = mLeft + child.getMeasuredWidth(); if (mX >= mWidth) { mX = childw; mY += childh; j = i; mLeft = 0; mRight = mLeft + child.getMeasuredWidth(); mTop = mY + 5; // PS:如果发现高度还是有问题就得自己再细调了 } mBottom = mTop + child.getMeasuredHeight(); mY = mTop; // 每次的高度必须记录 否则控件会叠加到一起 position.left = mLeft; position.top = mTop + 5; position.right = mRight; position.bottom = mBottom; map.put(child, position); } setMeasuredDimension(mWidth, mBottom); } @Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(0, 0); // default of 1px spacing } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); Position pos = (Position) map.get(child); if (pos != null) { child.layout(pos.left, pos.top, pos.right, pos.bottom); } else { } } } private class Position { int left, top, right, bottom; } public int getPosition(int IndexInRow, int childIndex) { if (IndexInRow > 0) { return getPosition(IndexInRow - 1, childIndex - 1) + getChildAt(childIndex - 1).getMeasuredWidth() + 5; } return getPaddingLeft(); }}
然后在布局里面一样使用:
<com.ds.platform.view.MyLinearLayout android:id="@+id/side_slip" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </com.ds.platform.view.MyLinearLayout>
注意这边一定要设置android:orientation=”horizontal”,否则不显示。
TextView垂直排列,代码如下:
package com.ds.platform.view;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.Gravity;import android.widget.TextView;/** * @author: Allen * @date: 2017/3/23 * @description: 垂直排列文字 */public class MyTextView extends TextView { final boolean topDown; public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); final int gravity = getGravity(); if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) { setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP); topDown = false; } else topDown = true; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } @Override protected boolean setFrame(int l, int t, int r, int b) { return super.setFrame(l, t, l + (b - t), t + (r - l)); } @Override public void draw(Canvas canvas) { if (topDown) { canvas.translate(getHeight(), 0); canvas.rotate(90); } else { canvas.translate(0, getWidth()); canvas.rotate(-90); } canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE); super.draw(canvas); }}
用法跟普通TextView一样,这边给自己做一个笔记。
3 0
- Android——自定义LinearLayout自动换行,TextView垂直排列。
- Android 自定义TextView 自动换行
- Android LinearLayout 自动换行
- Android LinearLayout 自动换行
- android textview自动换行、排列错乱问题及解决
- Android 自动换行的LinearLayout
- android TextView 自动换行
- Android TextView自动换行
- Android TextView 自动换行
- Android TextView自动换行
- android textview 自动换行
- 自定义ViewGroup——圆形排列LinearLayout
- 自定义Textview显示自动换行
- 自定义控件-MultipleTextView(自动换行、自动补齐宽度的排列多个TextView)
- Android LinearLayout中TextView标签项自动换行的一种解决方案
- Android LinearLayout中TextView标签项自动换行的一种解决方案
- Android LinearLayout中TextView标签项自动换行的一种解决方案(二)
- Android LinearLayout中TextView标签项自动换行的一种解决方案
- 如何拿到半数面试公司Offer——我的Python求职之路
- Pixhawk原生固件PX4之MAVLink外部通讯
- 写给自己
- POJ 3617 Best Cow Line(贪心+最小字典序)
- for...in 语句
- Android——自定义LinearLayout自动换行,TextView垂直排列。
- Python 爬虫知识汇总 以及40*问题解决策略
- 【干货】机器学习常用35大算法盘点
- SQL基础
- 使用Eclipse搭建Android开发环境
- 2016年(第15届)中国软件业务收入前百家企业名单(zz)
- Xen dom0激活xen_netback dynamic debug (netdev_dbg)
- Android中View.setPressed是怎么出现按压效果的
- MyBatis懒加载问题