11

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

例如,图片显示红色背景图标,红色背景显示待阅读的消息数量,如果没有消息,则显示灰色背景。

在此处输入图像描述

谢谢,

4

2 回答 2

20

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

  • 我不是 UI 专家,因此以下 XML 中可能存在一些无用/错误的内容。
  • 从我上面所说的来看,我没有设法在图标的右下角进行计数。:)
  • 对于测试,我使用icon.png来自 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>

主页小部件实际上是一个普通视图,您可以像构建活动一样在 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 和 Service 的组合

编辑:

如果您想要的计数应该在 aActivity而不是 a内,请参见下面的活动测试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);
    }
}
于 2011-04-09T11:37:48.423 回答
1

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

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

希望这可以帮助,

菲尔·莱洛

于 2011-04-06T17:22:47.210 回答