简体   繁体   English

Android主屏幕小部件(图标,标签 - 样式)

[英]Android Home Screen Widget (icon, label - style)

I'm trying to create an icon/widget (1 cell x 1 cell) that can be placed on the home screen of android. 我正在尝试创建一个可放置在android主屏幕上的图标/小部件(1个单元格x 1单元格)。 The widget will look and act exactly like the other standard shortcuts in android. 该小部件的外观和行为与android中的其他标准快捷方式完全相同。 It will have an icon and under that a label, it will be selectable with the trackball (highlight able) it will be highlighted when it is selected/clicked. 它将有一个图标,在该标签下,它可以通过轨迹球(高亮显示)进行选择,它将在选择/点击时突出显示。

How do I go about creating this home screen widget? 如何创建此主屏幕小部件?

Do I have to create the widget myself using code/xml or is there some standard xml, style, theme, code that I can use to ensure that the widget will have the same style/theme as the other home screen widgets? 我是否必须使用code / xml自己创建窗口小部件,或者是否有一些标准的xml,样式,主题和代码可用于确保窗口小部件与其他主屏幕窗口小部件具有相同的样式/主题?

I currently have the following 我目前有以下内容

res/drawable/corners.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/layout/widget.xml 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>

The resulting widget looks some what close, but its not selectable, it doesn't get highlighted when clicked and the label isn't exactly in the correct location or the correct style. 生成的窗口小部件看起来有些接近,但它不可选择,单击时不会突出显示,并且标签不完全位于正确的位置或正确的样式。

Any ideas, if there is a correct way to do this, or should I just keep working away on the above until I am closer? 任何想法,如果有正确的方法,或者我应该继续努力,直到我更接近?

The "correct way to do this" is to make a shortcut, and not try to mimic it with an app widget. “执行此操作的正确方法”是创建快捷方式,而不是尝试使用应用小部件来模仿它。 This has been pointed out repeatedly by the core Android team (notably Romain Guy) on the [android-developers] discussion list, such as : 核心Android团队(特别是Romain Guy)在[android-developers]讨论列表中反复指出过, 例如

Widgets should look like widgets, not like shortcuts. 小部件应该看起来像小部件,而不是快捷方式。 The main reason is that there is absolutely NO guarantee about what a shortcut will look like. 主要原因是绝对不能保证快捷方式的样子。 Other devices (especially ones with custom system UIs like MOTOBLUR or HTC Sense) might have a different look and feel. 其他设备(特别是具有自定义系统UI的设备,如MOTOBLUR或HTC Sense)可能具有不同的外观和感觉。 Or in the next update of Android we might change the way shortcuts are presented. 或者在Android的下一次更新中,我们可能会更改快捷方式的显示方式。

To make your item consistent with the system look and feel is not hard by referencing the system attribute android:attr/selectableItemBackground . 通过引用系统属性android:attr/selectableItemBackground来使您的项目与系统外观一致并不难。 For example, if you want to make an ImageView in your widget look "selectable" with proper highlighting etc: just do 例如,如果您想在窗口小部件中使用适当的突出显示等“可选择”,那么就这样做了

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

There are a lot in android_sdk_path/platforms/android-x . android_sdk_path/platforms/android-x中有很多。 Happy digging! 快乐挖!

EDIT: I found out later that this simple attribute does not live in SDK < v11. 编辑:我后来发现这个简单的属性不存在于SDK <v11中。 So the best way I know now is to download the appwidget template pack and use it. 所以我现在知道的最好的方法是下载appwidget模板包并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM