安卓:是否有可能更新的ImageView / ImageButton的用一个数字来显示新邮件的数量? [英] Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages?

查看:583
本文介绍了安卓:是否有可能更新的ImageView / ImageButton的用一个数字来显示新邮件的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS的,我们有一个功能,通过显示在图标的右上角有少数以更新与该用户的新挂起的消息应用程序图标,同样我也想知道,如果我们有方法更新ImageView的/的ImageButton的机器人(在这里我不希望更新的主屏幕上,但在我的应用程序的图标)。

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).

例如,在PIC显示读取的红色与待处理的消息的数量的红色背景的图标,如果没有消息它带有灰色背景显示。

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.

谢谢

推荐答案

我很感兴趣,这使我不知道该怎么做都不是。于是我开始寻找到它,这里是我做到了。

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.

  • 我不是一个专业的界面,因此可能有一些东西没有用/错在下面的XML。
  • 从我上面说的,我没有设法对图标的右下角计数。 :)
  • 有关测试,我用的是标准的的icon.png 从SDK
  • 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 /绘制/ shapecount.xml

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 /布局/ 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文件中建立一个像你会为一个活动。有一些规律可循,但。看到这个<一href="http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html">link为准则

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

在此链接这表明你如何建立一个AppWidget,你可以看到下面的code:

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);

其中, R.layout.appwidget_provider_layout 为您的家庭小部件的布局,这将是 my_widget_layout.xml

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);

请注意:

的<一个href="http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis">updatePeriodMillis犯规让小部件来要求更新一次以上,每30分钟,所以我使用的BroadcastReceiver和服务的组合

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

编辑:

请参阅下面的一个活性测试,如果你想计数应该是一个活动,而不是 AppWidget 之内。它使用相同的XMLS如上:

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);
    }
}

这篇关于安卓:是否有可能更新的ImageView / ImageButton的用一个数字来显示新邮件的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆