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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

LanTingShuXu   2021-03-22 我要评论
想了解Android小挂件(APP Widgets)设计指导的相关内容吗,LanTingShuXu在本文为您仔细讲解Android小挂件 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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的值将变成挂件默认大小的值,你可以使用minResizeWidthminResizeHeight来表示挂件真正的最小值,小于这个值时,控件将变得模糊和不可用。 

特别是那些基于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; 
    } 
  } 
} 

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们