更改默认小部件大小

Change default widget size

我正在开发 Android 屏幕小部件。默认大小(如果我将小部件从小部件列表拖放到我的主屏幕上)设置为 3x3,但我不知道在哪里可以更改它。谁能解释一下如何为另一个值设置默认值?

您在 appwidget-provider 中声明了 Widget 的默认大小(和其他属性)xml:

https://developer.android.com/guide/topics/appwidgets/index.html

特别是 minWidthminHeight 属性:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/preview"
    android:initialLayout="@layout/example_appwidget"
    android:configure="com.example.android.ExampleAppWidgetConfigure"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

The values for the minWidth and minHeight attributes specify the minimum amount of space the App Widget consumes by default. The default Home screen positions App Widgets in its window based on a grid of cells that have a defined height and width. If the values for an App Widget's minimum width or height don't match the dimensions of the cells, then the App Widget dimensions round up to the nearest cell size.

您可以设置以下代码:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:minWidth="146dp" 
   android:updatePeriodMillis="0" 
   android:minHeight="146dp" 
   android:initialLayout="@layout/activity_main">
</appwidget-provider>

如果您想了解详情。请拜访 https://www.tutorialspoint.com/android/android_widgets.htm

很简单,直接到res>XML>new_app_widget_info.xml,打开就可以找到

    android:minWidth="320dp"
    android:minHeight="72dp"
    android:previewImage="@drawable/example_appwidget_preview"

其中两行关于小部件大小,最后一行关于将在应用程序小部件中显示的图像。