我在一个Activity中完成了这项工作,它完美无缺.
ImageView myImage = (ImageView) findViewById(R.id.myImageView);
ShapeDrawable mDrawable;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
mDrawable.setIntrinsicWidth(width);
mDrawable.setIntrinsicHeight(height);
myImage.setImageDrawable(mDrawable);
Run Code Online (Sandbox Code Playgroud)
现在我想在一个小部件(内部onUpdate)中做同样的事情,我必须RemoteViews用来访问图像.
我怎么叫setImageDrawable的ImageView,从RemoteViews?所有远程视图方法似乎都需要Bitmap.
String ColorString = "Color.BLUE";
int colorint = Integer.parseInt(ColorString);
...
views.setTextColor(R.id.tvConfigInput, colorint);
Run Code Online (Sandbox Code Playgroud)
为什么会崩溃?在我得到的logcat中java.lang.numberformatexception: Invalid int "Color.BLUE"
我认为它在从字符串到int的转换中是错误的,因为如果我只是像这样设置int:
int colorint = Color.BLUE;
Run Code Online (Sandbox Code Playgroud)
它有效..但我不知道它有什么问题.
非常感谢
据我了解,android 小部件需要 RemoteViews,而不是 View,就像活动一样。
我的问题:有什么办法可以完全避免xml,用java代码构建整个布局?
替代问题:我可以以某种方式检索视图,就像我会使用的那样findViewById(int)吗?例如:LinearLayout linearLayout=(LinearLayout)findViewById(R.id.xxx);这样我就可以访问基本布局,并按照我想要的方式进行操作。
谢谢!
您好,我正在我的 Android 应用程序中显示自定义通知。但问题是自定义通知未显示。我看到了很多帖子,但没有找到与我的问题相关的任何答案。
我找不到问题出在哪里。你能看一下这个吗?
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// display the notification
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.notificationIcon, Util.notificationBitmap);
contentView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
contentView.setTextViewText(R.id.notificationContent, notificationMessage);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_logo)
.setContentTitle(Util.notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText(notificationMessage);
mBuilder.setContent(contentView);
// build notification
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify((int)SystemClock.currentThreadTimeMillis(), mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
自定义通知.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/notificationIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/notificationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notificationIcon"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/notificationContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/notificationTitle"
android:layout_toRightOf="@id/notificationIcon"
android:textAppearance="?android:attr/textAppearanceSmall" /> …Run Code Online (Sandbox Code Playgroud) 我的应用程序显示了一个小部件,共有 8 个按钮。我想让用户可以设置该小部件的样式并自定义按钮下方的 ImageViews 的大小。目的是让按钮保持原样,但动态更改 ImageView 大小。
为此,用户可以设置一个图标大小,它作为一个整数iconSize存储在 SharedPreference 中。
如何更改小部件中 ImageView 的大小?
现在,ImageViews 是使用 xml 文件中设置的大小创建的。如何使用其他尺寸实现重绘?
我认为这里没有太多代码可以发布,但如果有必要,我很乐意这样做,如果您知道哪些代码可以在这里提供帮助,请告诉我。
我不想做的事情:
调整整个小部件的大小
创建大量布局文件以根据 iconSize 进行切换
一些代码:
这就是我在活动中将 ImageView 大小设置为小部件预览的方式。icons是一个 ImageViews 数组,progress 指的是一个用于选择iconSize 的 ProgressBar。
for (ImageView icon : icons) {
icon.requestLayout();
// convert into actual Pixels
progress = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,
progress,
getResources().getDisplayMetrics()
);
// set width and height
icon.getLayoutParams().height = progress;
icon.getLayoutParams().width = progress;
sizeText.setText("" + progress);
}
Run Code Online (Sandbox Code Playgroud) 到目前为止,我一直在RemoteViews使用remoteViews.setImageViewBitmap(). 它总体上运行良好。
但是一些用户遇到了问题,我认为这是在加载非常大的位图时。无论如何,我已经将位图缓存到本地存储,所以我的想法是改用setImageViewUri()其他地方的建议。
但我无法让它工作......我只是得到一个空白的小部件。以下代码段说明了我在做什么(省略了一些上下文,但希望留下足够的内容)...
// Bitmap bmp is fetched from elsewhere... and then...
FileOutputStream fos = context.openFileOutput("img.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
// ... later ...
// this works...
remoteViews.setImageViewBitmap(R.id.imageView, bmp);
// but this doesn't...
remoteViews.setImageViewUri(R.id.imageView, Uri.fromFile(new File(context.getFilesDir().getPath(), "img.png")));
Run Code Online (Sandbox Code Playgroud)
编辑
尝试FileProvider按照以下 CommonsWare 的建议使用...
显现:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
xml/file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
代码:
File imagePath = new File(context.getFilesDir(), ".");
File newFile = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,用户可以在其中进行评论,并且正在向发布评论的用户推送通知。现在我有一个要求客户在通知中显示等级
我已经搜索一下,发现这个解决方案,定制通知,但现在我没有得到如何使用RatingBar。
我使用此类显示从Firebase控制台收到的自己的UI(RemoteViews)的通知。当应用程序是前台应用程序时,这很好用,但是当应用程序在后台运行时,通知以默认的设备样式显示。即使应用程序是前台或后台,我应该怎么做才能在自己的UI中显示通知?
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "Mehdi";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null)
{
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.payam_notification_layout);
contentView.setTextViewText(R.id.payam,remoteMessage.getNotification().getBody());
String channelId = "Default";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.status_icon_delivered)
.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.icon))
.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.notification))
.setCustomBigContentView(contentView)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, mBuilder.build());
} …Run Code Online (Sandbox Code Playgroud) 嘿所有当我的应用程序安装到模拟器时,我得到这个:
ERROR/AndroidRuntime(465):java.lang.RuntimeException:无法启动接收器com.myPackage.Widget.MYWidget:java.lang.SecurityException:不允许启动服务Intent {cmp = com.myPackage/.Widget.MYWidget $ MyWidgetService没有权限android.permission.BIND_REMOTEVIEWS
这是我的清单中的代码
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".Widget.MYWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!--Broadcast Receiver that will also process our self created action -->
<action android:name="com.temp.package.Widget.MYWidget.ACTION_WIDGET_LEFT_RECEIVER" />
<action android:name="com.temp.package.Widget.MYWidget.ACTION_WIDGET_PROGRESSBAR_RECEIVER" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/mywidget_widget_provider" />
</receiver>
<service android:name=".Widget.MYWidget$MyWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"
android:exported="true" />
<uses-permission
android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
Run Code Online (Sandbox Code Playgroud)
这是我的代码
public class MyWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent svcIntent = new Intent(context, MyWidgetService.class);
//svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
context.startService(svcIntent);
} …Run Code Online (Sandbox Code Playgroud) 此问题与C#使用编写的 Android 小部件有关Xamarin。
text style我想为bold小部件布局的元素设置文本视图。更改文本本身效果很好,但我无法设置其他属性。
这是构建更新的方法的一部分
remoteViews.SetTextViewText(Resource.Id.widget_info, nextItemText[1]);
if (element.InProgress)
{
// this is my try to set textstyle to bold
remoteViews.SetString(Resource.Id.widget_info, "textStyle", "bold");
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能设置text style为bold?
提前致谢!
我有一个使用 remoteView 的通知。在通知上我有 3 个按钮,每个按钮都有自己的待处理意图。出于某种原因,只有我使用 setOnClickPendingIntent 添加的最后一个 pendingIntent 有效。
这里有一些
Intent intentPause = new Intent(context, BluetoothConnectionService.class);
intentPause.putExtra("pause", device.device.getAddress());
Intent intentIncrease = new Intent(context, BluetoothConnectionService.class);
intentIncrease.putExtra("inc", device.device.getAddress());
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 0, intentIncrease, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setOnClickPendingIntent(R.id.noti_pause_heat_button, pendingIntentPause);
expandedView.setOnClickPendingIntent(R.id.noti_inc_heat_button, pendingIntentIncrease);
Notification notification = new Notification.Builder(context)
.setContentTitle(this.device.getName())
.setSmallIcon(R.drawable.inu_small)
.setContentText("" + this.notificationId)
.setContent(expandedView)
.build();
Run Code Online (Sandbox Code Playgroud) 我开发了一个小部件应用程序。每个小部件都有一个TextClock,我想为每个文本时钟设置时区,但RemoteViews不支持设置时区。
我如何设置时区?