一直不知道dashboard是个布局,其实有些应用用的挺多的,这种布局中文名叫仪表盘,就是把很多不同的功能,按一个一个不同的图标分列出来,而且这些图标的间距是xiangde感觉就有点像自由适应的gridview

不说了,先上图

6ec331c8fb9dbe3a5d82828e4ff1e60b.png

先看style.xml

fill_parent

50dp

horizontal

#ff0000

center_vertical

wrap_content

wrap_content

center_horizontal

2dp

16dp

bold

#ff29549f

@null

就是一个头部和每个button的属性,可以忽略

再者就是建立一个DashboardLayout,这个最重要,是大神写的,再次贴出来吧

public class DashboardLayout extends ViewGroup {

private static final int UNEVEN_GRID_PENALTY_MULTIPLIER = 10;

private int mMaxChildWidth = 0;

private int mMaxChildHeight = 0;

public DashboardLayout(Context context) {

super(context, null);

}

public DashboardLayout(Context context, AttributeSet attrs) {

super(context, attrs, 0);

}

public DashboardLayout(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

mMaxChildWidth = 0;

mMaxChildHeight = 0;

// Measure once to find the maximum child size.

int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(

MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);

int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(

MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);

final int count = getChildCount();

for (int i = 0; i < count; i++) {

final View child = getChildAt(i);

if (child.getVisibility() == GONE) {

continue;

}

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());

mMaxChildHeight = Math.max(mMaxChildHeight, child.getMeasuredHeight());

}

// Measure again for each child to be exactly the same size.

childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(

mMaxChildWidth, MeasureSpec.EXACTLY);

childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(

mMaxChildHeight, MeasureSpec.EXACTLY);

for (int i = 0; i < count; i++) {

final View child = getChildAt(i);

if (child.getVisibility() == GONE) {

continue;

}

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

}

setMeasuredDimension(

resolveSize(mMaxChildWidth, widthMeasureSpec),

resolveSize(mMaxChildHeight, heightMeasureSpec));

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int width = r - l;

int height = b - t;

final int count = getChildCount();

// Calculate the number of visible children.

int visibleCount = 0;

for (int i = 0; i < count; i++) {

final View child = getChildAt(i);

if (child.getVisibility() == GONE) {

continue;

}

++visibleCount;

}

if (visibleCount == 0) {

return;

}

// Calculate what number of rows and columns will optimize for even horizontal and

// vertical whitespace between items. Start with a 1 x N grid, then try 2 x N, and so on.

int bestSpaceDifference = Integer.MAX_VALUE;

int spaceDifference;

// Horizontal and vertical space between items

int hSpace = 0;

int vSpace = 0;

int cols = 1;

int rows;

while (true) {

rows = (visibleCount - 1) / cols + 1;

hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));

vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));

spaceDifference = Math.abs(vSpace - hSpace);

if (rows * cols != visibleCount) {

spaceDifference *= UNEVEN_GRID_PENALTY_MULTIPLIER;

}

if (spaceDifference < bestSpaceDifference) {

// Found a better whitespace squareness/ratio

bestSpaceDifference = spaceDifference;

// If we found a better whitespace squareness and there's only 1 row, this is

// the best we can do.

if (rows == 1) {

break;

}

} else {

// This is a worse whitespace ratio, use the previous value of cols and exit.

--cols;

rows = (visibleCount - 1) / cols + 1;

hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));

vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));

break;

}

++cols;

}

// Lay out children based on calculated best-fit number of rows and cols.

// If we chose a layout that has negative horizontal or vertical space, force it to zero.

hSpace = Math.max(0, hSpace);

vSpace = Math.max(0, vSpace);

// Re-use width/height variables to be child width/height.

width = (width - hSpace * (cols + 1)) / cols;

height = (height - vSpace * (rows + 1)) / rows;

int left, top;

int col, row;

int visibleIndex = 0;

for (int i = 0; i < count; i++) {

final View child = getChildAt(i);

if (child.getVisibility() == GONE) {

continue;

}

row = visibleIndex / cols;

col = visibleIndex % cols;

left = hSpace * (col + 1) + width * col;

top = vSpace * (row + 1) + height * row;

child.layout(left, top,

(hSpace == 0 && col == cols - 1) ? r : (left + width),

(vSpace == 0 && row == rows - 1) ? b : (top + height));

++visibleIndex;

}

}

} 接下来就是main.xml了

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:clickable="false"

android:paddingLeft="15dip"

android:scaleType="center"

android:src="@drawable/ic_launcher" />

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#f8f9fe" >

android:id="@+id/btn_news_feed"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="News Feed" />

android:id="@+id/btn_friends"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="Friends" />

android:id="@+id/btn_messages"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="Messages" />

android:id="@+id/btn_places"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="Places" />

android:id="@+id/btn_events"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="Events" />

android:id="@+id/btn_photos"

style="@style/DashboardButton"

android:drawableTop="@drawable/ic_launcher"

android:text="Photos" />

一个头部文件,然后就是引用的DashboardLayout,在里面有6个button

最后在入口acitivity加载出来,就是开始图片的效果了,但是不知道其他分辨率如何,但手里木有平板,只能在android graphical layout里面抓图,图有点小,将就看吧

ae6abbe68ffbb0dd12ebbd569047284a.png

看来无论多大的屏幕,都是等间距的,只不过图片的大小而已,到时候自己弄几个不同分辨率配置的图片,应该能达到想要的效果,我突然奇想,删掉一个button试试,看是什么布局

20090ec5330ec9d77b6f48fe57e059c3.png

上面这个是N7的尺寸,800X1200,布局不变,依然范特西

03f1c95557570807c64ce3d9d30b31a0.png

上面这个就是480X800的了,也就是开始第一次出现的那个尺寸,看来的确算是自适应了

总结:还没具体用过,但感觉如果是这种类似gridview的排列可以试试Dashboard布局,因为感觉方便,而且能够自适应

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐