英文:
Jetpack Compse Glance Widget API 33 is not resizable on API 30 works
问题
I'm creating app widget with glance. And on my personal phone API 30 resizable works on emulator API 33 or 32 resizable doesn't work.
Is that emulator issue? But Google Chrome widget resizable works.
xml/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:initialLayout="@layout/loader"
android:minWidth="80dp"
android:minHeight="80dp"
android:minResizeWidth="40dp"
android:minResizeHeight="40dp"
android:previewImage="@drawable/widget"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen" />
xml-v31/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:previewLayout="@layout/widget_preview"
android:targetCellWidth="3"
android:targetCellHeight="2"
android:description="@string/widget_description"/>
glance app widget
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceModifier
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.cornerRadius
import androidx.glance.background
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.state.GlanceStateDefinition
import androidx.glance.state.PreferencesGlanceStateDefinition
class MyWidget : GlanceAppWidget() {
override var stateDefinition: GlanceStateDefinition<*> = PreferencesGlanceStateDefinition
override val sizeMode: SizeMode = SizeMode.Responsive(
setOf(SMALL_SQUARE, HORIZONTAL_RECTANGLE, BIG_SQUARE)
)
@Composable
override fun Content() {
val size = LocalSize.current
val context = LocalContext.current
MyContent(
modifier = GlanceModifier
.fillMaxSize()
.background(ImageProvider(R.drawable.bg_widget))
.cornerRadius(16.dp)
.padding(8.dp)
)
}
companion object {
const val WATER_WIDGET_PREFS_KEY = "WATER_WIDGET_PREFS_KEY"
private val SMALL_SQUARE = DpSize(100.dp, 100.dp)
private val HORIZONTAL_RECTANGLE = DpSize(250.dp, 100.dp)
private val BIG_SQUARE = DpSize(250.dp, 250.dp)
}
}
class MyWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = MyWidget ()
}
AndroidManifest.xml
<receiver
android:name=".ui.widget.MyWidgetReceiver"
android:enabled="@bool/glance_appwidget_available"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
API 33
my widget | Google Chrome widget |
---|---|
API 30 on this phone I have resizable indicators and is also resizable.
英文:
I'm creating app widget with glance. And on my personal phone API 30 resizable works on emulator API 33 or 32 resizable doesn't work.
Is that emulator issue? But Google Chrome widget resizable works.
xml/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:initialLayout="@layout/loader"
android:minWidth="80dp"
android:minHeight="80dp"
android:minResizeWidth="40dp"
android:minResizeHeight="40dp"
android:previewImage="@drawable/widget"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen" />
xml-v31/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:previewLayout="@layout/widget_preview"
android:targetCellWidth="3"
android:targetCellHeight="2"
android:description="@string/widget_description"/>
glance app widget
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceModifier
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.cornerRadius
import androidx.glance.background
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.state.GlanceStateDefinition
import androidx.glance.state.PreferencesGlanceStateDefinition
class MyWidget : GlanceAppWidget() {
override var stateDefinition: GlanceStateDefinition<*> = PreferencesGlanceStateDefinition
override val sizeMode: SizeMode = SizeMode.Responsive(
setOf(SMALL_SQUARE, HORIZONTAL_RECTANGLE, BIG_SQUARE)
)
@Composable
override fun Content() {
val size = LocalSize.current
val context = LocalContext.current
MyContent(
modifier = GlanceModifier
.fillMaxSize()
.background(ImageProvider(R.drawable.bg_widget))
.cornerRadius(16.dp)
.padding(8.dp)
)
}
companion object {
const val WATER_WIDGET_PREFS_KEY = "WATER_WIDGET_PREFS_KEY"
private val SMALL_SQUARE = DpSize(100.dp, 100.dp)
private val HORIZONTAL_RECTANGLE = DpSize(250.dp, 100.dp)
private val BIG_SQUARE = DpSize(250.dp, 250.dp)
}
}
class MyWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = MyWidget ()
}
AndroidMAnifest.xml
<receiver
android:name=".ui.widget.MyWidgetReceiver"
android:enabled="@bool/glance_appwidget_available"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
API 33
my widget | Google Chrome widget |
---|---|
API 30 on this phone I have resizable indicators and is also resizable.
答案1
得分: 2
XML文件夹内的文件之间没有层次结构。这不像样式。
您可以选择在xml-v31中复制所有字段,或者将所有内容添加到基本XML中,并忽略更高API的警告。
英文:
There is no hierarchy between files inside the XML folder. It's not like styles.
You can either duplicate all fields in xml-v31 or just add everything in the base XML and ignore the warnings of higher API.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论