如何使用AppWidgetHostView在android应用程序上调整窗口小部件的大小?

时间:2018-11-14 15:21:31

标签: android android-appwidget android-appwidget-list

我创建了一个具有小部件的应用程序。我想调整窗口小部件的大小,但是不起作用。 小部件的大小在不包含 widgetView.updateAppWidgetSize(options,100,100,200,200)的情况下不变。为什么updateAppWidgetSize不起作用?如何更改小部件尺寸?

MainActivity:

class MainActivity : AppCompatActivity() {

    private lateinit var appWidgetManager: AppWidgetManager
    private lateinit var appWidegetHost: AppWidgetHost

    private val REQUEST_BIND_APPWIDGET = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        appWidgetManager = AppWidgetManager.getInstance(this)
        appWidegetHost = AppWidgetHost(this, 12345).apply {
            startListening()
        }

        val appWidgetId = appWidegetHost.allocateAppWidgetId()
        val appWidgetInfo = appWidgetManager.installedProviders.get(5)

        if (appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetInfo.provider)) {
            addAppWidget(appWidgetId, appWidgetInfo)
        } else {
            val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
            startActivityForResult(intent, REQUEST_BIND_APPWIDGET)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_BIND_APPWIDGET) {
                val appWidgetId = data!!.getIntExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID
                )
                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                    val appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId)
                    addAppWidget(appWidgetId, appWidgetInfo)
                }
            }
        } else { }
    }

    override fun onStop() {
        super.onStop()
        appWidegetHost.stopListening()
    }

    private fun addAppWidget(appWidgetId: Int, appWidgetInfo: AppWidgetProviderInfo) {
        val widgetView = appWidegetHost.createView(this, appWidgetId, appWidgetInfo)
        val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
        widgetView.updateAppWidgetSize(options, 100, 100, 200, 200)
        widgetView.setAppWidget(appWidgetId, appWidgetInfo)
        widgets.addView(widgetView)
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        tools:context=".MainActivity" android:orientation="vertical">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"     
                android:id="@+id/widgets">
        </LinearLayout>

 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

updateAppWidgetSize()方法仅影响最小和最大尺寸,不影响当前尺寸。

要更改窗口小部件的大小,请通过修改LayoutParams来实现与其他视图相同的方式:

widgetView.layoutParams = widgetView.layoutParams.apply {
    width = /* your width */
    height = /* your height */
}