編輯:關於Android編程
流式布局每行的行高以本行中最高的元素作為高,如果一個元素放不下到一行時直接到第二行


FlowLayoutView
package com.qf.sxy.customview05.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sxy on 2016/9/29.
* 流式布局 標簽
*/
public class FlowLayoutView extends ViewGroup{
public FlowLayoutView(Context context) {
super(context);
}
public FlowLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//或ViewGroup中所有子元素的屬性
//否則只獲取ViewGroup 的屬性
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(),attrs);
}
/**
* 測量控件本身的大小 寬和高 根據子內容獲取
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//獲取父布局的模式和尺寸
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//記錄Wrap_content的寬高
int width = 0;
int height = 0;
//沒一行的寬和高
int lineWidth = 0;
int lineHeight =0;
//獲取子元素的數量
int cCount = getChildCount();
//進行遍歷子元素
for(int i=0;igetWidth()-getPaddingLeft()-getPaddingRight()){//換行
width = Math.max(lineWidth,childWidth);
lineWidth = childWidth;//重新開一行 當前行的寬度
//疊加之前的高度
height += lineHeight;
//重新開一行 這個新行的高度
lineHeight = childHeight;
}else{//不換行
//記錄當前行的寬
lineWidth += childWidth;
//記錄一行中的高度
lineHeight = Math.max(lineHeight,childHeight);
}
if(i ==cCount-1){//判斷是否是最後一個元素
width = Math.max(width,lineWidth);
height += lineHeight;
}
//設置ViewGrop最終寬高
setMeasuredDimension(modeWidth==MeasureSpec.EXACTLY?sizeWidth:width+getPaddingRight()+getPaddingLeft(),
modeHeight==MeasureSpec.EXACTLY?sizeHeight:height+getPaddingBottom()+getPaddingTop());
}
}
//記錄所有子元素 按行存取
List> mAllViews = new ArrayList<>();
//記錄每一行中最大的高度
private List mLineHeight = new ArrayList<>();
//擺放子View ???????????????
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//為了多次執行時 數據正確
mAllViews.clear();
mLineHeight.clear();
//獲取父控件的寬度
int width = getWidth();
//記錄一行中的寬和高
int lineWidth = 0;
int lineHeight = 0;
//記錄一行中的 View
List lineView = new ArrayList<>();
//獲取子元素的個數
int cCount = getChildCount();
Log.e("AAA","==AAAA=>"+cCount);
//遍歷所有子元素
for(int i=0;iwidth-getPaddingLeft()-getPaddingRight()){//判斷是否換行
//記錄一行的高 和一行中的View
mLineHeight.add(lineHeight);
mAllViews.add(lineView);
Log.e("AAA","==>VVV===>"+mAllViews.size());
//讓數據重置
lineWidth = 0;
lineView = new ArrayList<>();
if (i == cCount - 1) {
lineView.add(child);
}
}else{
//記錄一行中的寬 高 view
lineWidth += childWidth;
lineHeight = Math.max(lineHeight,childHeight);
lineView.add(child);
}
}
//處理元素 數據以每行的形式處理
mLineHeight.add(lineHeight);
mAllViews.add(lineView);
//獲取每個View的padding
int left = getPaddingLeft();
int top = getPaddingTop();
//獲取有多少行
int lineNum = mAllViews.size();
Log.e("AAA","=BBB=>"+lineNum);
//展示每一行
for(int i=0;i
MainActivity
package com.qf.sxy.customview05; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.widget.TextView; import com.qf.sxy.customview05.widget.FlowLayoutView; public class MainActivity extends AppCompatActivity { private FlowLayoutView flowLayoutView; private String[]titils = {"sds","da","aa","fff","gggggg","hhgghg","jjhhhh","aa","fff","gggggg","hhgghg","jjhhhh","sds","da","aa","fff","gggggg","sds","da","aa","fff","gggggg"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flowLayoutView = ((FlowLayoutView) findViewById(R.id.flowLayoutView)); //5.fori for (int i = 0; i < titils.length; i++) { // Button btn = new Button(MainActivity.this); // btn.setText(titils[i]); // //設置Button屬性 內容包裹 // ViewGroup.MarginLayoutParams p= new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT); // flowLayoutView.addView(btn,p); TextView tv = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.tv,flowLayoutView,false); tv.setText(titils[i]); flowLayoutView.addView(tv); } } }activity_main.xml
tv.xml
tv_shape.xml
中國電信天翼4G+(CA)是什麼意思
中國電信發布了“天翼4G+”品牌,號稱比移動4G、聯通4G速度更快。4G才剛普及不久,現在又冒出來個4G+,4G+是什麼意思呢?需要
說說智能Android手機root的那些事
幫忙給朋友手機root ,是電信的定制機,試了很多軟件都沒有成,後來才發基帶版本是FB24,是不能root的,需要刷成EK21才能root,就在網上找刷機的
移植u-boot2012.04.1 -》2440 (二)norflash 識別
在上一篇文章中,我們實現了新建單板,時鐘 sdram 等一系列初始化工作,串口已經能正確輸出打印信息,但是有錯誤信息。現在,我們就來解決問題。搜索“Flash
Android與JS之間跨平台異步調用實例詳解
Android與JS之間跨平台異步調用 為什麼突然要搞這個問題呢? 在開發浏覽器的時候遇到這個狗血的問題,花了將近1天的時間才想到這個解決方案,Androi