編輯:關於 Android 程式設計
作用:為自己的應用找出 bug。使用步驟請參考官方文檔。

參照流程圖:當有子控件時,是否需要對子控件的大小進行布置?如果需要,就得重寫 onMeasure() 並呼叫 child.layout() 方法;若需要對子控件的布局進行控制,也要重寫 onLayout() 方法;若需要對控件的顯示進行控制,則要重寫 onDraw() 方法。一般來說,實現全部構造函數即可。
重寫:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//臨時top
int topmt=0;
int parentWidth = getMeasuredWidth();
//獲得孩子個數
int count =getChildCount();
for(int i=0;i
還需要重寫 onMeasure():
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//獲得父親的寬高
int widthsize=MeasureSpec.getSize(widthMeasureSpec);
int heightsize=MeasureSpec.getSize(heightMeasureSpec);
measureChildren(0, 0);//設置孩子,為0由父親安排寬高
setMeasuredDimension(widthsize, heightsize);
}
自定義布局的使用:
如果需要切換布局,一般定義一個 boolean 類型值進行切換。
FlowLayout
分析:多行擺放,單行若擺不下就換行擺放。
看下效果圖:

一開始看到這種布局說真的我無從下手,不知道該怎麼實現——每行個數不一樣、列數也不一樣。後來才知道自定義布局可以實現。根據上面 view 實現的流程圖可知,需要對控件的布局、大小進行控制,所以需要重寫 onLayout() 與 onMeasure() 方法。在重寫方法之前,需要先分析每一行是怎麼布局的,以物件導向的思想封裝每一行需要的屬性與方法,如下:
行的類
class Line{
//存儲孩子
private List mChildViews=new LinkedList();
private int usedWidth;//已經使用過的寬度
private int lineHeight;//行最大的高度
private int maxWidth;//行最大的寬度,父類給的
private int horizontalSpace;//中間的間隔
public Line(int maxWidth,int horizontalSpace){
this.maxWidth=maxWidth;
this.horizontalSpace=horizontalSpace;
}
//判斷該行是否能添加view
public boolean canAddView(View view){
//如果使用的寬度+准備加的View的寬度+中間的間隔>最大的寬度,加不上去
//准備加的View的寬度
int childwidth=view.getMeasuredWidth();
int size=mChildViews.size();
if(size==0){
return true;
}else if(usedWidth+childwidth+horizontalSpace>maxWidth){
return false;
}
return true;
}
//添加view到布局
public void addView(View view){
int childWidth=view.getMeasuredWidth();
int childHeight=view.getMeasuredHeight();
int size=mChildViews.size();
if(size==0){
//沒有孩子 已經使用的寬度
if(childWidth>maxWidth){
usedWidth=maxWidth;
}else{
usedWidth=childWidth;
}
//高度
lineHeight=childHeight;
}else{
//已經使用的寬度
usedWidth=usedWidth+childWidth+horizontalSpace;
//高度
lineHeight=lineHeight>childHeight?lineHeight:childHeight;
}
//加孩子
mChildViews.add(view);
}
//給行布局
public void layout(int left,int top) {
//給孩子布局
int size=mChildViews.size();
int tmpLeft=0;
//將每一行右側無法顯示的空白部分平分給每一行顯示的每個控件
int extraWidth=(int) ((maxWidth-usedWidth)*1f/size+0.5f);
for(int i=0;i0){
//希望孩子再寬點,填充右側空白
int widthMeasureSpec=MeasureSpec.makeMeasureSpec(childWidth+extraWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec=MeasureSpec.makeMeasureSpec(childHeight,MeasureSpec.EXACTLY);
child.measure(widthMeasureSpec, heightMeasureSpec);
//重新獲得寬高
childWidth=child.getMeasuredWidth();
childHeight=child.getMeasuredHeight();
}
int extraHeight=(int) ((lineHeight-childHeight)/2f+0.5f);
int l=left+tmpLeft;
int t=top+extraHeight;
int r=l+childWidth;
int b=t+childHeight;
child.layout(l, t, r, b);
//添加記錄
tmpLeft +=childWidth + horizontalSpace;
}
}
}
重寫 onLayout() 方法:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left=getPaddingLeft();
int top=getPaddingTop();
//讓行進行布局
for(int i=0;i
重寫 onMeasure() 方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//孩子個數記錄清空
mLines.clear();
mCurrentLine=null;
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int lineMaxWidth=widthSize-getPaddingLeft()-getPaddingRight();
//測量孩子完成時,就記錄到行裡面
int count=getChildCount();
for(int i=0;i0 && params.height>0){
child.measure(MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
}else{
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
if(mCurrentLine.canAddView(child)){
mCurrentLine.addView(child);
}else{
mLines.add(mCurrentLine);
mCurrentLine=new Line(lineMaxWidth, horizontalSpace);
mCurrentLine.addView(child);
}
}
mLines.add(mCurrentLine);
int measuredHeight=getPaddingTop()+getPaddingBottom();
for(int i=0;i
在 MainActivity 中:
public class MainActivity extends Activity {
private FlowLayout layout;
private String []mDatas={"單機游戲","美女","游戲","單機游戲","美女","淘寶","單機游戲","美女","淘寶","游戲" ,"單機游戲","淘寶","游戲","單機游戲","美女","淘寶","游戲","單機游戲","美女","淘寶","游戲" ,"有道","天貓","汽車商城","新聞","運動","熊出沒之大逃跑"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(FlowLayout) findViewById(R.id.flow_layout);
layout.setPadding(10, 10, 10, 10);
initData();
}
private void initData() {
for(int i=0;i
使用自定義布局時:
到這裡,上圖的效果就已經出來了。這種布局的邏輯判斷有點繁瑣,稍出差錯就顯示不出來,記得多看幾遍。
Android ActionBar 基本用法完整整理
本文整理自 Using the Android action bar (ActionBar) - Tutorial。一、ActionBar 簡介ActionBar 位於
百度地圖 SDK 實戰:顯示定位、POI 檢索與測距測面積
地圖顯示與定位 對任何一個地圖 SDK 來說,第一步都是把地圖畫出來,然後定位到當前城市。百度地圖與高德地圖在這一段的處理代碼幾乎如出一轍。以下以百度地圖為例: // 以
免權限實現檔案存取:Android 快取與應用目錄全攻略
在 Android 中,Context 提供了四個便捷的目錄方法,讓你能免開權限就完成檔案存取: getCacheDir、getFilesDir、getExternalC
打造高質感下拉刷新動畫:Android 自訂 RefreshView 實作
近來看到不少精緻又有質感的動畫效果,若能把這些動畫融入自己的專案,App 的整體觀感勢必加分不少。今天要帶大家拆解一個來自 Yalantis 的精美下拉刷新動畫。 先欣賞