【问题标题】:Creating iPhone-like badge notification on Android在 Android 上创建类似 iPhone 的徽章通知
【发布时间】:2014-04-14 07:23:51
【问题描述】:

所有,

无论我在哪里看到关于如何使其适用于应用程序图标的回复。我的情况有点不同。

在我的程序中,我有一个显示图像的 ListView。每个图像都与下面的对象相关联。

我想做的是创建一个类似于 iPhone 徽章通知的设计,但针对视图中的所有这些图像。

试图询问谷歌,我找到了this 链接。问题是 - 它不起作用。我正在使用 2.2 内核的 LG Android 手机上进行测试,我看到的只是一个小红点,它甚至没有位于图像本身高几个像素的左侧。

这是我的代码:

<?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>

有人可以看看吗?

我尝试更改页边距和文本大小,但没有任何改变。

谢谢。

【问题讨论】:

    标签: android layout notifications


    【解决方案1】:

    试试这个方法

    <?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>
    

    创建drawable/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>
    

    输出:

    【讨论】:

    • 我看到你使用 android:background 作为图像。这是否意味着我必须为我的图像选择背景?我正在从云端读取它们,目前正在使用 SetImageBitmap(),因此是这个问题。
    • 您必须将 Image 设置为 ImageView 的背景
    • 看来我必须制作一个 Drawable 对象才能与 setBackground() 调用一起使用。但我认为我不能这样做,因为我的图像(位图/png)不是来自资源。我该如何解决这个问题?还是除了setBackground()还有别的函数?
    • 尝试使用“new Drawable(my_bitmap);”表明此构造函数已被弃用。 :(
    • 你需要用 60x60 分辨率调整图片大小,然后设置为 ImageView 的位图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多