`

android dashboard布局的一个例子

阅读更多
在android中,有一类布局的样式,其实是不错的,叫dashboard,中文名叫仪表板
,其实就是把很多不同的功能,都按一个个不同的图标,分别列出来,而且这些图标的间距是相等的,如下图:
[img]
http://www.androidhive.info/wp-content/uploads/2011/12/output_dashboard.png
[/img]

   其核心为有一个头部header,一个中间部分,一个footer,在设计时,可以先搞个
style.xml,如下:
  
<resources>
    <style name="ActionBarCompat">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
 
    <style name="DashboardButton">
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:drawablePadding">2dp</item>
        <item name="android:textSize">16dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#ff29549f</item>
        <item name="android:background">@null</item>
    </style>    
 
   <style name="FooterBar">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">40dp</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">#dedede</item>
    </style>
</resources>



   然后头部的actionbar_layout.xml,可以这样写:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ActionBarCompat" > 
  
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:clickable="false"
        android:paddingLeft="15dip"
        android:scaleType="center"
        android:src="@drawable/facebook_logo" /> 
  
</LinearLayout>

    然后DashboardLayout.java 是GOOGLE IO提出的一个不错的程序,把应用各个图表分布均匀排列好,具体代码为:
package com.androidhive.dashboard; 
/* 
 * Copyright 2011 Google Inc. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
  
/** 
 * Custom layout that arranges children in a grid-like manner, optimizing for even horizontal and 
 * vertical whitespace. 
 */
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; 
        } 
    } 
}


  然后,这个其实是一个布局文件的样式程序,接下来就要设计其XML,利用这个布局程序,代码如下:
fragment_layout.xml

  
<com.androidhive.dashboard.DashboardLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#f8f9fe" > 
    <!--  News Feed Button -->
    <Button
        android:id="@+id/btn_news_feed"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_newsfeed"
        android:text="News Feed" /> 
  
    <!--  Friends Button -->
    <Button
        android:id="@+id/btn_friends"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_friends"
        android:text="Friends" /> 
  
    <!--  Messages Button -->
    <Button
        android:id="@+id/btn_messages"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_messages"
        android:text="Messages" /> 
  
    <!--  Places Button -->
    <Button
        android:id="@+id/btn_places"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_places"
        android:text="Places" /> 
  
    <!--  Events Button -->
    <Button
        android:id="@+id/btn_events"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_events"
        android:text="Events" /> 
  
    <!--  Photos Button -->
    <Button
        android:id="@+id/btn_photos"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_photos"
        android:text="Photos" /> 
  
</com.androidhive.dashboard.DashboardLayout>


  这样就可以初步运行了,这个是核心部分,更详细的文和代码,请见:
http://www.androidhive.info/2011/12/android-dashboard-design-tutorial/
2
0
分享到:
评论

相关推荐

    AndroidDashboard:教程Android仪表板

    在Android开发中,创建一个仪表板界面可以为用户提供直观的数据展示和操作入口。这个教程将引导你了解如何使用Java语言在Android平台上构建一个功能丰富的仪表板应用。 首先,我们需要理解仪表板的基本概念。一个...

    Dashboard-UI-Android:Android Studio中的简单Dashboard UI

    【Dashboard-UI-Android】是基于Android Studio开发的一个项目,主要展示了如何在Android平台上构建一个简洁的Dashboard用户界面。这个项目充分利用了Kotlin语言的优势,同时结合了Material Design设计原则和...

    在android实现仪表盘布局效果

    在Android平台上,创建一个仪表盘布局效果可以极大地提升应用程序的用户体验,尤其是在展示数据或状态时。Google开源的Android Dashboard是一个很好的资源,可以帮助开发者实现类似的效果。这个开源项目提供了丰富的...

    dashboard_javascript_dashboard模板_源码

    【标题】"dashboard_javascript_dashboard模板_源码" 指的是一个基于JavaScript技术构建的管理仪表盘模板,用于创建具有图表、数据表格等可视化元素的前端应用。这个模板为开发者提供了一个快速构建高效、直观的数据...

    Android例子源码使用AChartEngine的仪表盘

    在Android开发中,AChartEngine是一个非常实用的图表库,它允许开发者轻松地在应用程序中创建各种图表,如线图、柱状图、饼图以及本文提到的仪表盘。这个"Android例子源码使用AChartEngine的仪表盘"示例项目,旨在...

    k8s dashboard v2.7.0离线镜像包

    Kubernetes Dashboard 是 Kubernetes 集群管理的一个重要组件,它提供了基于 Web 的图形用户界面(GUI),使得集群操作和管理变得更加直观。v2.7.0 版本是 Kubernetes Dashboard 的一个重要更新,它包含了多项改进...

    dashboard_linux_arm64.zip

    标题中的"dashboard_linux_arm64.zip"表明这是一个适用于Linux ARM64架构的程序包,主要用于树莓派(Raspberry Pi)平台。树莓派是一种流行的单板计算机,因其小巧、功能强大且价格低廉,常被用于DIY项目和教育领域...

    Go-KubernetesDashboard是一个基于Web的Kubernetes集群管理UI

    而Kubernetes Dashboard则为这个功能丰富的平台提供了一个直观且易于使用的图形用户界面(GUI),使得非命令行用户也能高效地进行集群操作。 Kubernetes Dashboard的主要功能包括: 1. **应用管理**:用户可以通过...

    dashboard:一个React JS仪表板布局

    Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:yarn start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误...

    dashboard_html_dashboardweb_

    标题中的"dashboard_html_dashboardweb_"表明这是一个关于HTML构建的Web仪表盘项目,可能是一个用于数据可视化和监控的应用程序。Web仪表盘通常用于展示关键性能指标(KPIs),帮助用户快速理解系统的状态和趋势。 ...

    material-dashboard-dark-edition-v2.1.0.zip

    "material-dashboard-dark-edition-v2.1.0.zip" 是一个包含Material Dashboard Dark Edition v2.1.0的压缩包,这是一个专为大屏幕设计的响应式HTML模板,特别适用于数据展示和可视化的需求。该模板以其独特的黑色...

    k8s-dashboard镜像和yaml文件.rar

    Kubernetes Dashboard是一个Web UI,它提供了可视化的界面来查看和操作Kubernetes集群。以下是你可能需要了解的相关知识点: 1. **Kubernetes Dashboard组件**: - Kubernetes Dashboard由一个前端UI组件和后端API...

    Kubernetes Dashboard 部署.docx

    1. **创建服务账号**:创建一个名为 `admin-user` 的服务账号: ```bash kubectl create serviceaccount admin-user -n kubernetes-dashboard ``` 2. **绑定角色**:授予该服务账号管理员权限: ```bash ...

    sentinel-dashboard-1.7.2.jar.zip

    在本案例中,我们关注的是 Sentinel Dashboard 的 1.7.2 版本,这是一个用于可视化管理和配置 Sentinel 规则的控制台。 Sentinel Dashboard 是 Sentinel 的核心组件之一,它提供了一个用户友好的界面,允许开发者...

    Flex中通常使用布局组件DashBoard

    一个组件,相当一个容器,里面可以放好多似类面板的控件,这些控件在这个容器里面可以施动,最小化,最大化。

    DashBoard连接Webservice Demo

    在信息技术中,Dashboard通常指的是一个交互式的用户界面,它能够集中展示关键性能指标(KPIs)和业务数据。这些数据可以是实时的或历史的,旨在提供对组织运营状态的即时洞察。Dashboard的设计目标是简洁易懂,使...

    sleek-dashboard.rar

    其中一张图片作为效果图,展示了模板在实际应用中的外观,让开发者对模板的整体风格和布局有一个直观的认识。通过查看和理解这张效果图,开发者可以更好地了解模板的布局设计和功能模块。 除了主文件夹,压缩包可能...

    利用 django 及 pyecharts 做一个简单dashboard 展示页面

    本项目就是利用 django 及 pyecharts 做一个简单dashboard 展示页面。 只有1个模板页,可自行扩展。数据部分单独模块,无数据库访问代码,可修改为从数据库读取数据。有需要,可以联系我,代码中有邮箱地址。

    sentinel-dashboard 1.8.2.zip

    总的来说,Sentinel Dashboard 1.8.2 是一个强大且易用的工具,它为开发者提供了对分布式系统流量控制、熔断降级的全面管理,是构建高可用微服务架构的重要组成部分。通过熟练掌握 Sentinel Dashboard 的使用,...

    kubernetes-dashboard.yaml

    resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"] verbs: ["get", "update", "delete"] # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map. -...

Global site tag (gtag.js) - Google Analytics