Android小挂件(APP Widgets)设计指导_Android

应用小挂件(也叫做窗口小挂件)在android1.5的时候被第一次引出,后来再android3.0和android3.1中得到了极大的发展,他们可以展示一些应用的常用信息或者一些相关的信息到桌面上,标准的Android系统镜像中有很多自带的创口小挂件,例如:闹钟、音乐等

Figure 1. Example app widgets in Android 4.0.

本文将描述怎么去设计小挂件,以便于能很好的与其他挂件搭配的很默契,同时也会介绍一些小技巧。

AppWidget 剖析

       一个典型的android挂件将会包含三个组件部分:一个边界框、一个挂件图形控件、其他的元素。挂件包含了一部分安卓 View 控件的子集,他支持:textlabel、button、image。其他可用的组件见API Guide部分的Creating the app widget layout(见左侧)

       一个设计很好的挂件将会在边界框、框架之间留出一些外部边界,在内部的边界框中会留出一些内部边界。(也就是留出一些padding与margin)。如下图所示:

Figure 2. Widgets generally have margins between the bounding box and frame,and padding between the frame and widget control

Note:
在android4.0中,挂件将自动的与边框之间将上margin。

为你的挂件决定大小

        每一个挂件都必须指定minWidth 和 minHeight他表示默认最少需要多少的空间展示。当用户添加挂件到他的主屏幕时,通常占用的空间会大于你给的这两个值。Android的主屏幕提供给用户一种方格子的可用空间来放置应用图标或者桌面挂件。这种矩阵方格子在不同的设备上有不同的格式。比如说:一般手持设备提供4 X 4的格子。但是平板设备可以通过8 X 7的格子。当你的挂件被添加的时候,他将会根据minWidth和minHeight指定的宽高自动拉伸去占据最少的格子。使用.9.png图片作为背景和使用可伸展的布局可使你的挂件布局能很好的适配设备的主屏幕格子,以达到很好的使用体验。
        你设置的宽度、高度,或者说margin宽度都会有可能运行到不同的设备上,你可以使用下面列出的每个小格子占据的空间的数据来大致的估算你的挂件的最小尺寸。

最佳实践是将你的minWidth与minHeight设置相对保守,定义最小尺寸是可以使你的挂件渲染出很好的默认状态。

        比如说:假设你有个音乐播放器的挂件,他用作显示当前正在播放的专辑以及名字,我们就只需要一个播放按钮、一个下一曲按钮。

Figure 3. An example music player widget.

你最小的高度就应该为你的两个文本控件的高度+文本之间的margin高度和padding高度。你的最小宽度就应该为播放按钮最短宽度+ 下一曲按钮的最短宽度 + 文本的最短宽度(比如说最长10个字符)+水平的一些margin和padding距离

Figure4. Example sizes and margins for minWidth/minHeight calculations.We chose 144dp as an example good minimum width for the text labels.

最后的结果如下:

minWidth = 144dp + (2 × 8dp) + (2 × 56dp) = 272dp
minHeight = 48dp + (2 × 4dp) = 56dp

如果你使用的.9.png图片与内容有固有的padding距离,你也需要加上.

可调节大小的挂件

在android 3.1以后,挂件在水平方向与竖直方向都可以被调节大小,意味着:minWidth和minHeight的值将变成挂件默认大小的值,你可以使用minResizeWidth和minResizeHeight来表示挂件真正的最小值,小于这个值时,控件将变得模糊和不可用。 

特别是那些基于ListView或者GridView的集合类特征的挂件 

为你的挂件添加margin(外边界宽度)

       正如前面提到的,android4.0将可以为主屏幕的挂件自动添加小号、标准的外边界宽度(margin)。对于那些系统版本号在14或者以上的来说,为了平衡主屏幕的视觉,我们不推荐你再额外的添加margin到你的挂件外部。
        当然,对于那个更早一些的版本,添加自己的margin也不复杂,具体在API Guide中有介绍到。 

设计挂件的布局和背景图片

       很多挂件都只有一个固定的矩形背景或者圆角矩形的形状。其实最好的方法是使用.9.png图片来定义。(具体怎么使用.9.png图片,很简单这里不翻译了,自己去找资料学习) 

        对于挂件的内容部分,你应该使用可伸缩的布局方式。例如:RelativeLayout、LinearLayout、或者FrameLayout。这样可以让你的布局文件去适应很多种不同的屏幕尺寸。 

        下面是一个关于音乐播放的挂件的布局例子。他包含了一个文本域、一个暂停按钮、一个、下一曲按钮,他的margin取决于系统。

<FrameLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="@dimen/widget_margin"> 

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  android:background="@drawable/my_widget_background"> 

  <TextView
   android:id="@+id/song_info"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1" /> 

  <Button
   android:id="@+id/play_button"
   android:layout_width="@dimen/my_button_width"
   android:layout_height="match_parent" /> 

  <Button
   android:id="@+id/skip_button"
   android:layout_width="@dimen/my_button_width"
   android:layout_height="match_parent" />
 </LinearLayout>
</FrameLayout> 

如果你看了上面的例子和说明,你也可以开始做一个有弹性的布局

Figure 6. Excerpt flexible layouts and attributes.

使用挂件模板包

当你要开始设计一个新的挂件或者更新现有的挂件,你可以先看一下下面的设计模板。下面的包是可以下载的,他包含了.9.png背景图片和XML和一些针对不同像素密度的PS文件

下面是一个实例源代码

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * 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.
 */ 

package com.example.android.wiktionary; 

import com.example.android.wiktionary.SimpleWikiHelper.ApiException;
import com.example.android.wiktionary.SimpleWikiHelper.ParseException; 

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews; 

import java.util.regex.Matcher;
import java.util.regex.Pattern; 

/**
 * Define a simple widget that shows the Wiktionary "Word of the day." To build
 * an update we spawn a background {@link Service} to perform the API queries.
 */
public class WordWidget extends AppWidgetProvider {
  /**
   * Regular expression that splits "Word of the day" entry into word
   * name, word type, and the first description bullet point.
   */
  public static final String WOTD_PATTERN =
    "(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}"; 

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d("WordWidget.UpdateService", "onUpdate()");
    // To prevent any ANR timeouts, we perform the update in a service
    context.startService(new Intent(context, UpdateService.class));
  }
  public static class UpdateService extends Service {
    @Override
    public void onStart(Intent intent, int startId) {
      Log.d("WordWidget.UpdateService", "onStart()"); 

      // Build the widget update for today
      RemoteViews updateViews = buildUpdate(this);
      Log.d("WordWidget.UpdateService", "update built"); 

      // Push update for this widget to the home screen
      ComponentName thisWidget = new ComponentName(this, WordWidget.class);
      AppWidgetManager manager = AppWidgetManager.getInstance(this);
      manager.updateAppWidget(thisWidget, updateViews);
      Log.d("WordWidget.UpdateService", "widget updated");
    } 

    @Override
    public IBinder onBind(Intent intent) {
      return null;
    }
    /**
     * Build a widget update to show the current Wiktionary
     * "Word of the day." Will block until the online API returns.
     */
    public RemoteViews buildUpdate(Context context) {
      // Pick out month names from resources
      Resources res = context.getResources();
      String[] monthNames = res.getStringArray(R.array.month_names); 

      // Find current month and day
      Time today = new Time();
      today.setToNow(); 

      // Build the page title for today, such as "March 21"
      String pageName = res.getString(R.string.template_wotd_title,
          monthNames[today.month], today.monthDay);
      String pageContent = null; 

      try {
        // Try querying the Wiktionary API for today's word
        SimpleWikiHelper.prepareUserAgent(context);
        pageContent = SimpleWikiHelper.getPageContent(pageName, false);
      } catch (ApiException e) {
        Log.e("WordWidget", "Couldn't contact API", e);
      } catch (ParseException e) {
        Log.e("WordWidget", "Couldn't parse API response", e);
      } 

      RemoteViews views = null;
      Matcher matcher = null; 

        Prefs prefs = new Prefs(this);
      if (pageContent == null) {
        // could not get content, use cache
        // could be null
        pageContent = prefs.getPageContent();
      } 

      if (pageContent != null) {
        // we have page content
        // is it valid?
        matcher = Pattern.compile(WOTD_PATTERN).matcher(pageContent);
      }
      if (matcher != null && matcher.find()) {
        // valid content, cache it
        // ensure that latest valid content is
        // always cached in case of failures
        prefs.setPageContent(pageContent); 

        // Build an update that holds the updated widget contents
        views = new RemoteViews(context.getPackageName(), R.layout.widget_word); 

        String wordTitle = matcher.group(1);
        views.setTextViewText(R.id.word_title, wordTitle);
        views.setTextViewText(R.id.word_type, matcher.group(2));
        views.setTextViewText(R.id.definition, matcher.group(3).trim()); 

        // When user clicks on widget, launch to Wiktionary definition page
        String definePage = String.format("%s://%s/%s", ExtendedWikiHelper.WIKI_AUTHORITY,
            ExtendedWikiHelper.WIKI_LOOKUP_HOST, wordTitle);
        Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
            0 /* no requestCode */, defineIntent, 0 /* no flags */);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent); 

      } else {
        // Didn't find word of day, so show error message
        views = new RemoteViews(context.getPackageName(), R.layout.widget_message);
        views.setTextViewText(R.id.message, context.getString(R.string.widget_error));
      }
      return views;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索app
, Android小挂件
, Widgets
Android窗口挂件
android app widgets、android widgets、android misc widgets、app widgets、android时间挂件,以便于您获取更多的相关知识。

时间: 2024-08-02 13:55:18

Android小挂件(APP Widgets)设计指导_Android的相关文章

Android桌面插件App Widget用法分析_Android

本文实例讲述了Android桌面插件App Widget用法.分享给大家供大家参考,具体如下: 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget)是微小的应用程序视图,可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新.你可以通过一个App Widget provider来发布一个Widget.可以容纳其它App Widget的应用程序组件被称为App Widget宿主.下面的截屏显示了一个音乐App Widget. appwidget 这篇文章描述了如何使用Ap

Android开发5:应用程序窗口小部件App Widgets的实现(附demo)_Android

前言 本次主要是实现一个Android应用,实现静态广播.动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此次实验的重点是AppWidget小部件的实现啦~ 首先,我们简单说一下Widget是一个啥玩意~ 应用程序窗口小部件(Widget)是微小的应用程序视图,可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新.你可以通过一个App Widget provider来发布一个Widget.可以容纳其它App Widget的应用程序组件被称为App Widge

详解Android冷启动实现APP秒开的方法_Android

一.前言 在阅读这篇文章之前,首先需要理解几个东西: 1.什么是Android的冷启动时间?       冷启动时间是指用户从手机桌面点击APP的那一刻起到启动页面的Activity调用onCreate()方法之间的这个时间段. 2.在冷启动的时间段内发生了什么?       首先我们要知道当打开一个Activity的时候发生了什么,在一个Activity打开时,如果该Activity所属的Application还没有启动,那么系统会为这个Activity创建一个进程(每创建一个进程都会调用一次

Android桌面组件App Widget完整案例_Android

本文实例讲述了Android桌面组件App Widget用法.分享给大家供大家参考.具体如下: 这里模拟一个案例:把AppWidget添加到桌面后,点击AppWidget后AppWidget文本会轮回改变 main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

Android开发5:应用程序窗口小部件App Widgets的实现(附demo)

前言 本次主要是实现一个Android应用,实现静态广播.动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此次实验的重点是AppWidget小部件的实现啦~ 首先,我们简单说一下Widget是一个啥玩意~ 应用程序窗口小部件(Widget)是微小的应用程序视图,可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新.你可以通过一个App Widget provider来发布一个Widget.可以容纳其它App Widget的应用程序组件被称为App Widge

Android中的脑残设计总结_Android

Trackball轨迹球 这有点类似于PC上面的鼠标,可以用于导航,为此便有了Focus,但是这实际操作意义并不大,因为整个屏幕都是触控的,还用导航干什么,先把Focus用Trackball移动到某个控件再去点击这不是多此一举吗?而且这个Focus十分难以控制,给开发者添加了无尽的烦恼,所以轨迹球是脑残的设计,所以现在Android的设备已经进化的都去掉了轨迹球了.当初为何加入这个东西呢?我猜因为Android的原始设计是一直带有键盘的,或者是为了非触控屏幕准备的,如果有非触控,用一个类似鼠标的

ANDROID 完美退出APP的实例代码_Android

大家都知道 Android 的 Activity 是存着历史栈的,比如从 A -> B -> C,C 完成 finish 后回到 B,把所有的Activity 都 finish了,程序就自然退出了. 当然在 finish 的同时也需要是否自己程序的其他资源.所以需要想个办法把 Activity 给存起来.然后在程序退出的地方调用它们的 finish()方法. 使用全局变量.对了,第一个想到的就是继承 Application,代码入下: 复制代码 代码如下: public class Agent

Android桌面插件App Widget用法分析

本文实例讲述了Android桌面插件App Widget用法.分享给大家供大家参考,具体如下: 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget)是微小的应用程序视图,可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新.你可以通过一个App Widget provider来发布一个Widget.可以容纳其它App Widget的应用程序组件被称为App Widget宿主.下面的截屏显示了一个音乐App Widget. appwidget 这篇文章描述了如何使用Ap

Android仿微信主界面设计_Android

先来一张效果图 一.ActionBar的设计 首先是main.xml,先定义这些菜单,界面稍后在调整 <menu xmlns:android="http://schemas.android.com/apk/res/android" tools:context=".MainActivity"> <item android:id="@+id/action_search" android:actionViewClass="a