2010-04-16 93 views
4

我正在尝试创建可放置在主屏幕上的图标/小部件(1 cell x 1 cell)。这个小部件的外观和动作完全像android中的其他标准快捷方式。它会有一个图标,在它下面有一个标签,它可以通过跟踪球进行选择(突出显示),当它被选中/点击时它会被突出显示。Android主屏幕小部件(图标,标签样式)

我该如何着手创建这个主屏幕小部件?

我是否必须自己使用code/xml创建窗口小部件,还是有一些标准的xml,样式,主题,代码可用于确保窗口小部件与其他主屏幕窗口小部件具有相同的样式/主题?

目前,我有以下

RES /绘制/ corners.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Corners"> 
    <stroke android:width="4dp" android:color="#CC222222" /> 
    <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" /> 
    <corners android:radius="4dp" /> 
</shape> 

RES /布局/ widget.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Widget" 
    android:layout_width="72dip" 
    android:layout_height="72dip" 
    android:orientation="vertical" 
    android:focusable="true" 
    android:gravity="center_horizontal" 
    style="@android:style/Widget" 
    > 

    <ImageView 
     android:id="@+id/WidgetIcon" 
     android:src="@drawable/icon" 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:paddingTop="3dip" 
     android:gravity="center" 
     /> 

    <TextView 
     android:id="@+id/WidgetLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="@string/app_name" 
     android:textSize="15dip" 
     android:background="@drawable/corners" 
     /> 

</LinearLayout> 

产生的小部件看起来有些什么接近,但它不可选,单击时不会突出显示,标签也不完全在正确的位置或正确的样式。

任何想法,如果有一个正确的方法来做到这一点,或者我应该继续努力,直到我离得更近?

+0

你有没有找到一个解决方案,它将如何看起来像标准标签? – chrisonline 2011-06-15 08:58:35

+0

下面的答案是正确的,你不会假设小部件看起来像快捷方式,因为不同的操作系统可能会显示不同的快捷方式!因此,如果您为HTC手机制作一个,它可能与MOTOBLUR手机上显示快捷方式的方式不一致。说,我反正做了我的一个看起来像HTC :) – dmulligan 2011-07-20 17:01:46

回答

9

“正确的做法”是制作一个快捷方式,而不是试图用应用程序小部件模仿它。这已被多次指出由核心Android团队(尤其是罗曼盖伊)中的[Android的开发者]讨论列表上,such as

窗口小部件应该看起来像小部件,而不是像 快捷方式。主要原因是 ,绝对没有保证 关于快捷方式的样子。 其他设备(特别是带有MOTOBLUR或HTC Sense的自定义系统UI)的设备可能会有不同的外观和 的感觉。或者在Android 的下一次更新中,我们可能会更改提供的快捷方式 的方式。

+1

看起来像1x1小部件的创建是唯一的方式来创建应用程序图标与'柜台徽章'。有什么办法可以将设备特定的主题应用于我的布局吗? – 2011-04-14 19:28:08

+0

@Kevin:根据我对“声明式”的定义,无论是应用程序小部件还是快捷方式都不能声明性声明。 – CommonsWare 2012-11-21 14:46:02

+0

您如何获得小部件的名称,以便在主屏幕上显示在小部件下面,就像它为应用程序一样? – toobsco42 2015-05-17 16:22:04

1

通过引用系统属性android:attr/selectableItemBackground来使您的项目与系统外观一致并不困难。例如,如果你想在你的widget的ImageView的期待“选择”适当高亮等:只是做

<ImageView 
    ... 
    android:background="?android:attr/selectableItemBackground" 
    ... 
    /> 

有很多android_sdk_path/platforms/android-x。快乐挖!

编辑:我后来发现,这个简单的属性不住在SDK < v11。所以我现在知道的最好的方法是下载appwidget模板包并使用它。