您的位置:首页 > 其它

Widget桌面小部件

2016-08-30 00:17 162 查看
1、需要在清单文件中配置元数据:

2、配置当前元数据要配置的xml文件;

res/xml 自建xml文件 下的.xml文件。

3、需要配置一个广播接收者;

4.实现一个桌面小部件的xml;

li:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/example_appwidget"  //元数据中的初始化layout。   需要注意的是请看下面:初始
android:minHeight="40dp" //最小高度                                           化的layout

android:minWidth="200dp"    //注意这个最小宽度不能过大,如大于屏幕宽度不会消失的。
android:updatePeriodMillis="0" >

</appwidget-provider>


demo:

<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data    //这个就是元数据。
android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />  //res/xml/下de.xml文件。
</receiver>


初始化的layout:就是metadata中的 initialLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:background="#f00"
android:layout_width="match_parent"  <!-- 当宽度设置为matchparent时显示的是metadata中的最小宽度 -->
android:layout_height="wrap_content"
android:text="你好啊"
android:textColor="@android:color/black"/>

</LinearLayout>


请参考:

https://developer.android.com/guide/topics/appwidgets/index.html#MetaData
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: