Я создал приложение с виджетом.Я хотел бы изменить размер виджета, но он не работает.Размер виджета не изменяется между w / o и 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>