自定义View(通过ViewGroup实现,Textview长度根据字数的多少而定的)
2017-06-07 15:13
405 查看
一、布局
public class FlowLayout extends ViewGroup {
}
public class MainActivity extends AppCompatActivity {
}
“`
1.1、Drawable文件布局 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#5522ff"/> <corners android:radius="10dp"/> <padding android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp" /> </shape> 1.2、Main文件布局 <com.baway.www.custom_viewdemo.FlowLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/flowlayot"> </com.baway.www.custom_viewdemo.FlowLayout> 二、帮助类
public class FlowLayout extends ViewGroup {
//存储所有子View private List<List<View>> mlistView = new ArrayList<>(); //每一行的高度 private List<Integer> mlistHigth = new ArrayList<>(); public FlowLayout(Context context) { this(context,null); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //如果当前ViewGroup的宽高为wrap_content的情况 ,自己测量的宽高 int width = 0; int hight = 0; //每一行线的宽度 int linWidth = 0; int linHight = 0; //父控件传进来的宽度和高度以及对应的测量模式 int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modeHight = MeasureSpec.getMode(heightMeasureSpec); int sizeHight = MeasureSpec.getSize(heightMeasureSpec); //获取子View的个数 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); //测量子view的宽高 measureChild(child,widthMeasureSpec,heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //子view占得宽度 int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; //子VIew的高度 int childHight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; //换行判断 if ((linWidth + childWidth) > sizeWidth){ //对比得到最大的宽度 width = Math.max(width,linWidth); linWidth = childWidth; //记录行高 hight += linHight; linHight = childHight; }else{ //不换行的情况 linWidth += childWidth; //得到最大行高 linHight = Math.max(linHight,childHight); } //处理最后一个子View的情况 if (i == childCount -1){ width = Math.max(width,linWidth); hight += linHight; } } setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width , modeHight == MeasureSpec.EXACTLY ? sizeHight : hight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mlistView.clear(); mlistHigth.clear(); //获取当前ViewGroup的宽度 int width = getWidth(); int linWidth = 0; int linHight = 0; //记录当前行的view List<View> lineViews = new ArrayList<View>(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth(); int childHight = child.getMeasuredHeight(); //需要换行 if (childWidth + linWidth + lp.leftMargin + lp.rightMargin > width){ //记录LineHeight mlistHigth.add(linHight); //记录当前的Views mlistView.add(lineViews); linWidth = 0; linHight = childHight + lp.topMargin + lp.bottomMargin; lineViews = new ArrayList(); } linWidth += childWidth + lp.leftMargin + lp.rightMargin; linHight = Math.max(linHight,childHight + lp.topMargin + lp.bottomMargin); lineViews.add(child); } //处理最后一行 mlistHigth.add(linHight); mlistView.add(lineViews); //设置VIew的位置 int left = 0; int top = 0; //获取行数 int lineCount = mlistView.size(); for (int i = 0; i < lineCount; i++) { lineViews = mlistView.get(i); linHight = mlistHigth.get(i); for (int j = 0; j < lineViews.size(); j++) { View child = lineViews.get(j); if (child.getVisibility() == View.GONE){ continue; } MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int cLeft = left + lp.leftMargin; int cTop = top + lp.topMargin; int cRight = cLeft + child.getMeasuredWidth(); int cBottom = cTop + child.getMeasuredHeight(); child.layout(cLeft,cTop,cRight,cBottom); left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } left = 0; top += linHight; } } // @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); }
}
三、主方法MainActivity
public class MainActivity extends AppCompatActivity {
//设置数据的个数 private String mName[] = {"xListView","XUtils","PullToRefresh","ImageLoader","RecycleView","自定义View","ViewGroup","TextView","Button","ImageButton", "ImageView","ProgressBar","SeekBar","Model","View","Presenter","大磨叽","艳艳"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initChildViews(); } private void initChildViews() { FlowLayout mFlowLayout = (FlowLayout) findViewById(R.id.flowlayot); int wrapContent = LayoutParams.WRAP_CONTENT; MarginLayoutParams lp = new MarginLayoutParams(wrapContent,wrapContent); lp.leftMargin = 5; lp.rightMargin = 5; lp.topMargin = 5; lp.bottomMargin = 5; for (int i = 0; i < mName.length; i++) { TextView view = new TextView(this); view.setText(mName[i]); view.setTextColor(Color.WHITE); //view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg)); view.setBackgroundDrawable(getResources().getDrawable(R.drawable.twos)); mFlowLayout.addView(view,lp); } }
}
“`
相关文章推荐
- android自定义实现textview限制多少行数以后以省略号结尾。 ... 这种省略号不是
- android开发 textview根据字数长度自动调整字体大小
- 通过自定义ViewGroup来实现侧滑菜单效果,解决滑动冲突
- 自定义ViewGroup 实现拖动跟快速滚动的效果
- android之自定义ViewGroup和自动换行的布局的实现
- UILabel根据字数多少自动实现适应高度
- Android-自定义TextView和异步加载图片的实现
- 重写AutoCompleteTextView之实现自定义匹配规则 及 提示
- android:自定义viewgroup,并实现滚动条和换行。
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- android之自定义ViewGroup和自动换行的布局的实现
- AutoCompleteTextView 实现自定义匹配规则提示
- Android 自定义AlertDialog并通过AlertDialog修改TextView的值
- 当时遇到的主要难点在于TextView的内容不会刷新改变值,今天终于通过Timer和Handler实现了,分享给大家
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- AutoCompleteTextView 实现自定义匹配规则提示
- 通过改写ArrayAdapter 用AutoCompleteTextView 实现汉字和拼音双向关联
- UILabel根据字数多少自动实现适应高度
- android 自定义textView,实现排版对齐和换行
- Android-自定义TextView和异步加载图片的实现