Android:是否可以用数字更新 ImageView/ImageButton 以显示新消息的数量?

发布于 2024-10-30 22:55:54 字数 283 浏览 0 评论 0原文

在 iOS 中,我们有一个功能,可以通过在图标的右上角显示一个小数字来为用户更新应用程序图标,并添加新的待处理消息,类似地,我想知道我们是否有方法来更新 ImageView/ImageButton在android中(这里我不想更新主屏幕上的图标,而是更新我的应用程序内的图标)。

例如,该图片显示红色背景图标,其中以红色显示待读取的消息数量,如果没有消息,则显示为灰色背景。

在此处输入图像描述

谢谢,

In iOS we have a feature to update the app icon with the new pending messages for the user by showing a small number on the right-top corner of the icon and similarly I would like to know if we have method to update a ImageView/ImageButton in android(Here I am not looking to update the icon on the home screen but inside my app).

For example, the pic shows the red background icon with the number of messages pending to be read in red and if there are no messages it shows with a gray background.

enter image description here

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-11-06 22:55:54

我对这个很感兴趣,因为我也不知道怎么做。所以我开始研究它,这就是我的做法。

  • 我不是 UI 方面的专家,因此以下 XML 中可能有一些无用/错误的内容。
  • 根据我上面所说,我没有设法在图标的右下角进行计数。 :)
  • 为了进行测试,我使用 sdk 中的标准 icon.png

res/drawable/shapecount.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <corners android:radius="20dp"  />    
  <solid android:color="#ff2233" />
</shape>

res/layout/ my_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<RelativeLayout
    android:orientation="vertical"
    android:background="@null"
    android:id="@+id/rlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
            android:id="@+id/icon"
            android:src="@drawable/icon" 
            android:layout_margin="0dp"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="50" android:textSize="9dp" android:textStyle="bold"  
        android:background="@drawable/shapecount"
        android:textColor="#FFFFFF"
        android:paddingLeft="3dp" android:paddingRight="3dp"
            android:layout_margin="0dp"
        android:layout_alignBottom="@+id/rlayout"
        android:id="@+id/txtCount" />

</RelativeLayout>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="My App Name" android:textSize="9dp" android:textStyle="bold"  
    android:background="@drawable/shapecount"
    android:textColor="#FFFFFF"
    android:paddingLeft="3dp" android:paddingRight="3dp"
        android:layout_margin="0dp"
    android:layout_alignBottom="@+id/rlayout"
    android:id="@+id/txtAppName" />
 </LinearLayout>

主页小部件实际上是一个普通视图,您可以像在 Activity 中一样在 XML 文件中构建它。但有一些规则需要遵循。请参阅此链接获取指南

在此链接向您展示了如何构建AppWidget,您可以看到以下代码

// Get the layout for the App Widget and attach an on-click listener to the button
  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);

R.layout.appwidget_provider_layout 是您的主页小部件的布局,它将是 my_widget_layout.xml

从该 views 中,您可以设置您的值想要任何计数文本视图:

int count = 50;//Or whatever value you set to it.
views.setTextViewText(R.id.txtCount,Integer.toString(count));

然后您只需更新小部件本身:

appWidgetManager.updateAppWidget(appWidgetId, views);

注意:

updatePeriodMillis 不允许小部件每 30 分钟请求更新一次以上,因此我使用组合BroadcastReceiver 和服务

编辑:

如果您想要的计数应该在 Activity 而不是 AppWidget 内,请参阅下面的活动测试。它使用与上面相同的 XML:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */

    final Random gen = new Random();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView t = (TextView) findViewById(R.id.txtCount);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() { 
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        int a = gen.nextInt(20);
                        t.setText(Integer.toString(a));                     
                    }
                });
                }
            }
        ,new Date(), 3000L);
    }
}

I was interested in this SO as I didnt know how to do it neither. So I started looking into it and here is how I did it.

  • I am not a specialist in UI so there may be some stuff useless/wrong in the following XML.
  • From what I said above, I didnt manage to have the count on the bottom right corner of the icon. :)
  • For test, I use the standard icon.png from the sdk

res/drawable/shapecount.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <corners android:radius="20dp"  />    
  <solid android:color="#ff2233" />
</shape>

res/layout/my_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<RelativeLayout
    android:orientation="vertical"
    android:background="@null"
    android:id="@+id/rlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
            android:id="@+id/icon"
            android:src="@drawable/icon" 
            android:layout_margin="0dp"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="50" android:textSize="9dp" android:textStyle="bold"  
        android:background="@drawable/shapecount"
        android:textColor="#FFFFFF"
        android:paddingLeft="3dp" android:paddingRight="3dp"
            android:layout_margin="0dp"
        android:layout_alignBottom="@+id/rlayout"
        android:id="@+id/txtCount" />

</RelativeLayout>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="My App Name" android:textSize="9dp" android:textStyle="bold"  
    android:background="@drawable/shapecount"
    android:textColor="#FFFFFF"
    android:paddingLeft="3dp" android:paddingRight="3dp"
        android:layout_margin="0dp"
    android:layout_alignBottom="@+id/rlayout"
    android:id="@+id/txtAppName" />
 </LinearLayout>

An home widget is actually a normal view that you can build in an XML file like you would for an activity. There is some rules to follow though. See this link for guideline

In this link which shows you how to build an AppWidget, you can see the following code:

// Get the layout for the App Widget and attach an on-click listener to the button
  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);

where R.layout.appwidget_provider_layout is the layout of your home widget, which will be my_widget_layout.xml

From that views, you can set the value you want for any count text view:

int count = 50;//Or whatever value you set to it.
views.setTextViewText(R.id.txtCount,Integer.toString(count));

and then you just have to update the widget itself:

appWidgetManager.updateAppWidget(appWidgetId, views);

NOTE:

The updatePeriodMillis doesnt allow the widget to request an update more than once every 30 min so I use a combination of BroadcastReceiver and Service

EDIT:

See below a activity test if the count you want should be within an Activity instead of a AppWidget. It uses the same XMLs as above:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */

    final Random gen = new Random();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView t = (TextView) findViewById(R.id.txtCount);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() { 
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        int a = gen.nextInt(20);
                        t.setText(Integer.toString(a));                     
                    }
                });
                }
            }
        ,new Date(), 3000L);
    }
}
初见你 2024-11-06 22:55:54

当您希望在应用程序内部而不是在主屏幕上执行此操作时,我建议您只创建一个 ImageView 子类,并重写 onDraw() - 执行任何背景绘制,调用 super.onDraw(),然后执行前景绘画。

或者,您可以使用 Bitmap.copy 等从基本图标构建合成图像,以及带有消息计数的较小位图。

希望这有帮助,

菲尔·莱洛

As you're looking to do this inside your app, not on the home screen, I'd suggest you just create an ImageView subclass, and override onDraw() - do any background painting, call super.onDraw(), then do foreground painting.

Alternatively, you could use Bitmap.copy etc. to build a composite image from the base icon, and a smaller bitmap with the message count.

Hope this helps,

Phil Lello

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文