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

标签 android android-widget

我正在尝试创建一个可以放置在 android 主屏幕上的图标/小部件(1 个单元格 x 1 个单元格)。该小部件的外观和行为与 android 中的其他标准快捷方式完全一样。它将有一个图标,在该图标下方有一个标签,可以使用轨迹球选择它(能够突出显示),当它被选中/单击时它会突出显示。

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

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

我目前有以下内容

res/drawable/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

<?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>

生成的小部件看起来有些接近,但它不可选择,单击时不会突出显示,并且标签不在正确的位置或正确的样式中。

任何想法,如果有正确的方法来做到这一点,还是我应该继续努力直到我更接近?

最佳答案

“执行此操作的正确方法”是创建快捷方式,而不是尝试使用应用小部件来模仿它。 Android 核心团队(特别是 Romain Guy)在 [android-developers] 讨论列表中反复指出了这一点,such as :

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. Or in the next update of Android we might change the way shortcuts are presented.

关于Android 主屏幕小部件(图标、标签 - 样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2651631/

相关文章:

android - 禁用 SearchView

使用 VideoView 显示视频时的 android-black 屏幕

Android 模拟器在一段时间后失去互联网连接

java - DialogFragment 中 subview 的 getWidth

android - 将 jQuery Mobile 与phonegap 结合使用

java - 获取Android onClickListener的返回值

android - 权限拒绝 : not allowed to send broadcast android. intent.action.HEADSET_PLUG

java - 小部件点击无响应

android - 设置小部件布局的 Alpha/Opacity

android - 如何在android的 TextView 中对长文本进行文本换行