【问题标题】:Android - How to add Notification Counter on the right above of ImageViewAndroid - 如何在 ImageView 的右上方添加通知计数器
【发布时间】:2017-08-08 11:52:46
【问题描述】:

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

我想要这样的通知计数器:

这是我的 activity_menu 布局:

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

这是我的 menu.java 类:

public class Menu extends AppCompatActivity {

            private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                    = new 

BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.nav_about:
                        return true;
                    case R.id.nav_location:
                        return true;
                    case R.id.nav_phone:
                        return true;
                    case R.id.nav_home:
                        return true;
                }
                return false;
            }
        };

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_menu);

            }
    }

请帮助找到解决此问题的方法。我搜索了很多答案,但没有找到针对我的案例的解决方案。提前致谢!

【问题讨论】:

标签: java android android-layout notifications counter


【解决方案1】:

像这样在 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>

【讨论】:

    【解决方案2】:
    <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>
    

    【讨论】:

    • 阿维纳什?它正在与 textview 一起使用,但无法设置徽章(。你能帮忙吗
    【解决方案3】:

    RelativeLayout 将您的图像包裹在GridLayout 中,并将通知指示器放在RelativeLayout 中。 示例:

    <RelativeLayout
         android:layout_width="90dp"
         android:layout_height="90dp">
    <ImageView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:src="@drawable/cases"
         android:layout_marginLeft="60dp"
         android:layout_marginTop="40dp"
         android:id="@+id/img_cases"/>
    <TextView
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:background="#FF0000"/>
    </RelativeLayout>
    

    您可以通过以下方式更新它们

    GridLayout gridLayout.... 
    TextView item1 = (TextView) gridLayout.findViewById(R.id.item1);
    item1.setText("3"); // 3 notifications 
    

    【讨论】:

    • 感谢对 textview 的帮助。但是如何在图标上方制作徽章计数器?
    【解决方案4】:

    您可以按如下方式使用约束布局:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多