gpt4 book ai didi

java - 如何在调整大小时设置不同的小部件图标

转载 作者:行者123 更新时间:2023-12-01 09:25:30 25 4
gpt4 key购买 nike

我有一个用于不同屏幕尺寸的小部件图标,放在文件夹drawable-mdpi/drawable-xhdpi等中。

假设设备具有 mdpi 屏幕,因此 Android 将使用 drawable-mdpi/widget_pic.png [48x48px] 显示我的 1x1 小部件

当我将小部件大小调整为 2x2 时,它将使用相同的图标,但会放置在中心而不缩放。
我希望 Android 看到小部件尺寸变大了 2 倍,并相应地从资源中选择一个图标(即 drawable-xhdpi/widget_pic.png[96x96px])。

所以我的问题是如何做到这一点?
我可以想到一个解决方案,但它似乎有缺陷:
捕获onAppWidgetOptionsChanged()事件并替换layout/image_src。
但是我不知道在哪里可以获得2x图标。


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<application>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myWidgetXml"/>
</receiver>
</application>
</manifest>

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/myLayout"
android:initialLayout="@layout/myLayout"
android:minHeight="40dp"
android:minWidth="40dp"
android:resizeMode="horizontal"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen">
</appwidget-provider>

myLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:layout_margin="0dp"
android:gravity="center"
android:background="@android:drawable/alert_dark_frame"
android:orientation="vertical">

<ImageButton
android:id="@+id/WidgetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/widget_pic" />
<TextView
android:id="@+id/WidgetTextView"
android:textColor="@android:color/white"
android:textSize="14sp"
android:layout_marginTop="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>

MyWidget.java

public class MyWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds)
updateAppWidget(context, appWidgetManager, appWidgetId);
}

private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout);
Intent intent = new Intent(context, MainActivity.class);
Uri.Builder builder = new Uri.Builder();
intent.setData(builder.build());
intent.setAction(Intent.ACTION_VIEW);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic);
views.setTextViewText(R.id.WidgetTextView, "text");
_appWidgetManager.updateAppWidget(WidgetId, views);
}
}

最佳答案

实际上,不可能告诉 android 在运行时更改资源
因为它在应用安装期间加载它们
所以有两个选项:

  • 使用 android:scaleType="fitCenter"android:adjustViewBounds="true" 缩放图像
  • 每个 drawable-* 文件夹中有 2 组资源
    (在我的例子中,它是 widget_pic1x1.pngwidget_pic2x2.png)

关于java - 如何在调整大小时设置不同的小部件图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com