建立在Android与iPhone类似徽章的通知 [英] Creating iPhone-like badge notification on Android

查看:121
本文介绍了建立在Android与iPhone类似徽章的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我到处看看我看到如何使它的应用程序图标的工作答复。我的情况有点不同。

Everywhere I look I see a reply on how to make it work for application icon. My situation is a little different.

在我的节目,我有一个ListView,其显示图像。每个图像与下方的对象相关联。

In my program I have a ListView which displays images. Every image is associated with the object underneath.

我想要做的就是创建像在iPhone徽章通知的设计,但在视图中的所有这些图像。

What I want to do is create a design like in the iPhone badge notification, but for all those images in the view.

试着问谷歌,我发现<一href="http://tech.chitgoks.com/2012/07/06/how-to-add-a-notification-alert-badge-in-an-android-imageview/"相对=nofollow>这个链接。问题是 - 这是行不通的。我测试,LG的Andr​​oid手机与2.2内核和所有我看到的是红色的小圆点是连位于图像本身的像素夫妇走高和左侧。

Trying to ask Google, I found this link. Problem is - it does not work. I'm testing on the LG Android phone with 2.2 kernel and all I see is the small red dot which is not even located on the image itself couple of pixels higher and to the left.

下面是我的code:

<?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/user_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/user_image_description"
        android:src="@drawable/placeholder"
    />
    <TextView
        android:id="@+id/user_messages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/icon"
        android:layout_alignBottom="@id/icon"
        android:background="@color/red"
        android:textColor="@color/silver"
        android:layout_marginLeft="-4dp"
        android:layout_marginBottom="-4dp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:gravity="center"
    />
</RelativeLayout>

有人可以看看吗?

Can someone please look?

我试图更改页边距和文本大小,但它并没有改变任何东西。

I tried to change the margins as well as text size, but it didn't change anything.

感谢你。

推荐答案

试试这个方法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:focusable="true" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:layout_marginTop="8dp"
    android:background="@drawable/logo"
    android:contentDescription="image"
    android:scaleType="center" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/icon"
    android:gravity="center"
    android:paddingLeft="3dp"
    android:paddingTop="10dp"
    android:shadowColor="#000000"
    android:shadowDx="1"
    android:shadowDy="1"
    android:shadowRadius="1.5"
    android:text="@string/app_name"
    android:textColor="#FFF" />

<TextView
    android:id="@+id/txt_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-10dip"
    android:layout_toRightOf="@+id/icon"
    android:background="@drawable/badge_count2"
    android:contentDescription="badge"
    android:gravity="center"
    android:text="1"
    android:textColor="@color/White"
    android:textStyle="bold"
    android:visibility="visible" />

</RelativeLayout>

创建绘制/ badge_count2.xml 文件中包含

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="@color/red" >
</solid>

<stroke
    android:width="2dp"
    android:color="#FFFFFF" >
</stroke>

<padding
    android:bottom="2dp"
    android:left="7dp"
    android:right="7dp"
    android:top="3dp" />

<corners android:radius="10dp" >
</corners>

输出:

Output:

这篇关于建立在Android与iPhone类似徽章的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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