创建 widget 窗口小组件

随着android的手机屏幕越来越大,为了丰富屏幕内容 app widget(窗口小组件)被越来越多的应用所使用。app widget 有什么好处呢?它可以在不启动应用程序的情况下,让用户在屏幕上有一块交互窗口和程序入口点。

这是我手机自带的天气预报app widget效果。

 

为了创建一个应用程序的widget我们需要创建三个组件:

(1)给widget创建一个布局资源。

res/layout/nview.xml

<?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="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开网页" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开照相机" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开电话本" />

</LinearLayout>
View Code

Widget的布局不是支持所有Android组件的。目前仅支持一下组件。

FrameLayout 
LinearLayout 
RelativeLayout 

AnalogClock 
Button 
Chronmeter 
ImageButton 
ImageView 
ProgressBar 
TextView 

如果使用了weidget不支持的组件。那么在Widget创建时会导致android.view.InflateExceptionn异常 

 

 

(2)创建widget描述文件。该文件是xml类型。位于res/xml中

res/xml/my_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/nview"
    android:minHeight="40dp"
    android:minWidth="200dp"
    android:previewImage="@drawable/ic_launcher"
    android:resizeMode="horizontal|vertical"
    android:configure="com.example.testdemo.MainActivity"
    android:updatePeriodMillis="360000" >

</appwidget-provider>
View Code

appwidget-provider标签能够描述widget元数据。该元数据使用以下属性来描述weiget

initialLayout: widget的布局资源。

minWidth/minHeight: widget的最小宽度和最小高度。

resizeMode: (这个属性是在Android 3.1中引入的)通过给resizeMode设置horizontal和vertical的组合,允许在你指定的方向上调整widget的大小,设置为none会禁止调整大小。

lable: 小组件标题,在小组件列表中能看到。

updatePeriodMillis: widget更新最小周期,单位毫秒。Android将会以该设定的时间唤醒设备,来更新主屏幕上的widget,这个时间周期不能小于30分钟。最好每天更新一到两次

icon: 你的widget 在小组件管理器中的图标。你可以指定icon为你的weiget的缩略图。方便用户了解你的widget样子。

configure:当widget添加到屏幕中时,可以启动一个Activity,该Acitvity可以对widget进行设置,该属性必须使用Activity的完全限定名,如上代码中的。

perviewImage: (Android3.0中引入的)设置widget在小组件管理器中的预览效果。而不是显示其图标icon。其实这个和icon差不多。最好还是用这个。

 

(3)创建一个继承于AppWidgetProvider的Intent接受类,AppWidgetProvider封装了对Intent的处理,并提供了更新,删除,启用,禁用等事件的处理程序,在该类中我们可以从服务器端获得数据并进行处理并在Widget中显示等。

package com.example.testdemo;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class MyAppWidget extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // Widget组件被删除时触发
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        // 最后一个Widget组件关闭时触发
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        // 第一个Widget组件启动时触发 
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // 没接收一次广播消息就调用一次,使用频繁,不管什么操作都最先触发该函数 
        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // 当更新widget时调用
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}
View Code

 

 

 

(4)向AndroidManifest.xml中注册

 

  <receiver android:name=".MyAppWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/my_widget" >
            </meta-data>
        </receiver>
View Code

注意:使用的是Receiver标签,为了把一个broadcaset Receiver 指定为一个 app widget。需要添加以下两个标签。

1.一个用于"android.appwidget.action.APPWDIGET_UPDATE" 动作的intent filter。

2.一个对描述文件widget的xml引用。这个文件描述了widget的设置。

 

更多属性参加Android api

转载于:https://www.cnblogs.com/ywtk/p/3849010.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中,可以使用布局(Layout)来实现组件窗口大小变化的效果。布局是一种自动调整组件位置和大小的机制,可以使组件随着窗口大小的变化自动调整。 Qt提供了多种布局方式,常用的有水平布局(QHBoxLayout),垂直布局(QVBoxLayout)和网格布局(QGridLayout)。 以垂直布局为例,以下是实现组件窗口大小变化的示例代码: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget *widget = new QWidget; QVBoxLayout *layout = new QVBoxLayout(widget); // 创建垂直布局,并设置widget的布局 QLabel *label1 = new QLabel("Label 1"); QLabel *label2 = new QLabel("Label 2"); QLabel *label3 = new QLabel("Label 3"); layout->addWidget(label1); // 将label1添加到布局中 layout->addWidget(label2); // 将label2添加到布局中 layout->addWidget(label3); // 将label3添加到布局中 widget->setLayout(layout); // 设置widget的布局 widget->show(); return app.exec(); } ``` 在上述示例代码中,创建了一个QWidget对象和一个垂直布局对象,并将垂直布局设置为QWidget对象的布局。然后创建了三个QLabel对象,并将它们添加到垂直布局中。最后将QWidget对象的布局设置为垂直布局。 这样,当QWidget对象的大小发生变化时,垂直布局会自动调整QLabel对象的位置和大小,从而实现了组件窗口大小变化的效果。 如果要实现更复杂的布局,可以使用多个布局嵌套的方式来实现。比如可以将多个水平布局或垂直布局嵌套到一个网格布局中,来实现更灵活的布局方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值