Java Android-如何在ImageView的右上方添加通知计数器

Java Android-如何在ImageView的右上方添加通知计数器,java,android,android-layout,notifications,counter,Java,Android,Android Layout,Notifications,Counter,我希望在Imageview的右侧有一个具有类似通知计数器的应用程序:以下是我的布局照片: 我希望有这样的通知计数器: 以下是我的活动菜单布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto

我希望在Imageview的右侧有一个具有类似通知计数器的应用程序:以下是我的布局照片:

我希望有这样的通知计数器:

以下是我的活动菜单布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="datasite.com.konnex.Menu"
    android:background="#ffffff">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3fc0ea">

        <ImageButton
            android:layout_width="120dp"
            android:layout_height="38dp"
            android:background="@drawable/lg1"
            android:layout_marginLeft="130dp" />

        <SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="90dp"/>
    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

      <GridLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:columnCount="2">
         <ImageView
             android:layout_width="90dp"
             android:layout_height="90dp"
             android:src="@drawable/cases"
             android:layout_marginLeft="60dp"
             android:layout_marginTop="40dp"
             android:id="@+id/img_cases"/>
          <ImageView
              android:layout_width="90dp"
              android:layout_height="90dp"
              android:src="@drawable/invoices"
              android:layout_marginLeft="80dp"
              android:layout_marginTop="40dp" />

          <ImageView
              android:layout_width="90dp"
              android:layout_height="90dp"
              android:src="@drawable/announcement"
              android:layout_marginLeft="60dp"
              android:layout_marginTop="40dp" />
          <ImageView
              android:layout_width="90dp"
              android:layout_height="90dp"
              android:src="@drawable/users"
              android:layout_marginTop="40dp"
              android:layout_marginLeft="80dp"/>
          <ImageView
               android:layout_width="90dp"
               android:layout_height="90dp"
               android:src="@drawable/document"
              android:layout_marginTop="60dp"
              android:layout_marginLeft="60dp" />
          <ImageView
              android:layout_width="90dp"
              android:layout_height="90dp"
              android:src="@drawable/meetings"
              android:layout_marginTop="60dp"
              android:layout_marginLeft="80dp"/>

      </GridLayout>
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fffafa"
        app:menu="@menu/navigation"
        app:itemIconTint="@color/dark"
        app:itemTextColor="@color/dark" />

</LinearLayout>
请帮助找到这个问题的解决办法。我搜索了很多答案,但没有找到适合我的案例的解决方案。提前谢谢


<RelativeLayout
    android:layout_width="100dp"
    android:layout_height="100dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fc0505"
        android:text="10"
        android:layout_alignParentRight="true"
        android:textSize="18sp"/>

</RelativeLayout>

使用
RelativeLayout
将图像包装在
GridLayout
中,并将通知指示器放入RelativeLayout中。 例如:


使用
RelativeLayout
将图像包装在
GridLayout
内,并将通知指示器放入RelativeLayout内。 例如:


像这样在FrameLayout中包装每个ImageView

<GridLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnCount="2">
      <FrameLayout
          android:layout_width="90dp"
          android:layout_height="90dp">
          <ImageView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:src="@drawable/cases"/>
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="1"
             android:background="@android:color/red"
             android:layout_gravity="top|end"/>
      </FrameLayout>
</GridLayout>

像这样在FrameLayout中包装每个图像视图

<GridLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:columnCount="2">
      <FrameLayout
          android:layout_width="90dp"
          android:layout_height="90dp">
          <ImageView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:src="@drawable/cases"/>
         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="1"
             android:background="@android:color/red"
             android:layout_gravity="top|end"/>
      </FrameLayout>
</GridLayout>

您可以使用约束布局,如下所示:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/filter"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_baseline_filter_list_24px" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circular_border"
        android:gravity="center"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        card_view:layout_constraintStart_toEndOf="@string/filter"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintTop_toTopOf="@id/filter"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item xmlns:android="http://schemas.android.com/apk/res/android">
        <shape android:shape="oval">
            <solid android:color="#DD2C00" />
            <stroke android:color="#FFFFFF" android:width="1dp" />
            <size
                android:height="15dp"
                android:width="15dp" />
        </shape>
    </item>
</ripple> 

添加一个可绘制的资源文件,用于向计数器添加背景,如下所示:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/filter"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_baseline_filter_list_24px" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circular_border"
        android:gravity="center"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        card_view:layout_constraintStart_toEndOf="@string/filter"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintTop_toTopOf="@id/filter"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item xmlns:android="http://schemas.android.com/apk/res/android">
        <shape android:shape="oval">
            <solid android:color="#DD2C00" />
            <stroke android:color="#FFFFFF" android:width="1dp" />
            <size
                android:height="15dp"
                android:width="15dp" />
        </shape>
    </item>
</ripple> 

您可以使用约束布局,如下所示:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/filter"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_baseline_filter_list_24px" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circular_border"
        android:gravity="center"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        card_view:layout_constraintStart_toEndOf="@string/filter"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintTop_toTopOf="@id/filter"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item xmlns:android="http://schemas.android.com/apk/res/android">
        <shape android:shape="oval">
            <solid android:color="#DD2C00" />
            <stroke android:color="#FFFFFF" android:width="1dp" />
            <size
                android:height="15dp"
                android:width="15dp" />
        </shape>
    </item>
</ripple> 

添加一个可绘制的资源文件,用于向计数器添加背景,如下所示:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/filter"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_baseline_filter_list_24px" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circular_border"
        android:gravity="center"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        card_view:layout_constraintStart_toEndOf="@string/filter"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintTop_toTopOf="@id/filter"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item xmlns:android="http://schemas.android.com/apk/res/android">
        <shape android:shape="oval">
            <solid android:color="#DD2C00" />
            <stroke android:color="#FFFFFF" android:width="1dp" />
            <size
                android:height="15dp"
                android:width="15dp" />
        </shape>
    </item>
</ripple> 


您可以尝试此库您可以尝试此库谢谢textview的帮助。但是如何在图标上方制作徽章计数器呢?谢谢textview的帮助。但如何在图标上方制作徽章计数器?Avinash?它正在使用textview,但无法获取设置徽章(.a)你能帮我这样做吗?它正在使用textview,但无法获取设置徽章(.a)你能帮我这样做吗