[英]Android Home Screen Widget (icon, label - style)
我正在尝试创建一个可放置在android主屏幕上的图标/小部件(1个单元格x 1单元格)。 该小部件的外观和行为与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>
生成的窗口小部件看起来有些接近,但它不可选择,单击时不会突出显示,并且标签不完全位于正确的位置或正确的样式。
任何想法,如果有正确的方法,或者我应该继续努力,直到我更接近?
“执行此操作的正确方法”是创建快捷方式,而不是尝试使用应用小部件来模仿它。 核心Android团队(特别是Romain Guy)在[android-developers]讨论列表中反复指出过, 例如 :
小部件应该看起来像小部件,而不是快捷方式。 主要原因是绝对不能保证快捷方式的样子。 其他设备(特别是具有自定义系统UI的设备,如MOTOBLUR或HTC Sense)可能具有不同的外观和感觉。 或者在Android的下一次更新中,我们可能会更改快捷方式的显示方式。
通过引用系统属性android:attr/selectableItemBackground
来使您的项目与系统外观一致并不难。 例如,如果您想在窗口小部件中使用适当的突出显示等“可选择”,那么就这样做了
<ImageView
...
android:background="?android:attr/selectableItemBackground"
...
/>
android_sdk_path/platforms/android-x
中有很多。 快乐挖!
编辑:我后来发现这个简单的属性不存在于SDK <v11中。 所以我现在知道的最好的方法是下载appwidget模板包并使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.