Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。

huangapple go评论61阅读模式
英文:

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
Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。 Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。

API 30 on this phone I have resizable indicators and is also resizable.

Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。

英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;appwidget-provider xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:description=&quot;@string/app_name&quot;
    android:initialLayout=&quot;@layout/loader&quot;
    android:minWidth=&quot;80dp&quot;
    android:minHeight=&quot;80dp&quot;
    android:minResizeWidth=&quot;40dp&quot;
    android:minResizeHeight=&quot;40dp&quot;
    android:previewImage=&quot;@drawable/widget&quot;
    android:resizeMode=&quot;horizontal|vertical&quot;
    android:widgetCategory=&quot;home_screen&quot; /&gt;

xml-v31/widget_info.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;appwidget-provider xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:previewLayout=&quot;@layout/widget_preview&quot;
    android:targetCellWidth=&quot;3&quot;
    android:targetCellHeight=&quot;2&quot;
    android:description=&quot;@string/widget_description&quot;/&gt;

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&lt;*&gt; = 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 = &quot;WATER_WIDGET_PREFS_KEY&quot;

        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

        &lt;receiver
            android:name=&quot;.ui.widget.MyWidgetReceiver&quot;
            android:enabled=&quot;@bool/glance_appwidget_available&quot;
            android:exported=&quot;false&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.appwidget.action.APPWIDGET_UPDATE&quot; /&gt;
            &lt;/intent-filter&gt;

            &lt;meta-data
                android:name=&quot;android.appwidget.provider&quot;
                android:resource=&quot;@xml/widget_info&quot; /&gt;
        &lt;/receiver&gt;

API 33

my widget Google Chrome widget
Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。 Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。

API 30 on this phone I have resizable indicators and is also resizable.

Jetpack Compose Glance Widget API 33 在 API 30 上不可调整大小,仅在 API 30 上有效。

答案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.

huangapple
  • 本文由 发表于 2023年2月24日 05:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定