package org.qtt.view;
import org.qtt.main.ui.ApplicationActivity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
public class MyPhoneCard extends ViewGroup {
private static final int COLUMNS = 3;
private static final int ROWS = 5;
private static final int NUM_BUTTON = COLUMNS * ROWS;
private View[] mButtons = new View[NUM_BUTTON];
private int mButtonWidth;
private int mButtonHeight;
private int mPaddingLeft;
private int mPaddingRight;
private int mPaddingTop;
private int mPaddingBottom;
private int mWidthInc;
private int mHeightInc;
private int mWidth;
private int mHeight;
public MyPhoneCard(Context context) {
super(context);
}
public MyPhoneCard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPhoneCard(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final View[] btns = mButtons;
for (int i = 0; i < NUM_BUTTON; i++) {
btns[i] = this.getChildAt(i);
btns[i].measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
}
final View child = btns[0];
// mButtonWidth = child.getMeasuredWidth();
// mButtonHeight = child.getMeasuredHeight();
/**
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width= dm.widthPixels;
height = dm.heightPixels;
*/
mButtonWidth =ApplicationActivity.width/COLUMNS ;//获取屏幕的宽度/总的列数(需要获取屏幕宽度)
mButtonHeight =(ApplicationActivity.height-ApplicationActivity.height*1/3)/ROWS;//
mPaddingLeft = this.getPaddingLeft();
mPaddingRight = this.getPaddingRight();
mPaddingTop = this.getPaddingTop();
mPaddingBottom = this.getPaddingBottom();
mWidthInc = mButtonWidth + mPaddingLeft + mPaddingRight;
mHeightInc = mButtonHeight + mPaddingTop + mPaddingBottom;
mWidth = mWidthInc * COLUMNS;
mHeight = mHeightInc * ROWS;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = resolveSize(mWidth, widthMeasureSpec);
final int height = resolveSize(mHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
final View[] buttons = mButtons;
int i = 0;
int y = (bottom - top) - mHeight + mPaddingTop;
for (int row = 0; row < ROWS; row++) {
int x = mPaddingLeft;
for (int col = 0; col < COLUMNS; col++) {
buttons[i].layout(x, y, x + mButtonWidth, y + mButtonHeight);
x = x + mWidthInc;
i++;
}
y = y + mHeightInc;
}
}
}