Android - 如何在ImageView的右上方添加通知计数器
Android - How to add Notification Counter on the right above of ImageView
我想在 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 class:
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);
}
}
请帮助找到解决此问题的方法。我搜索了很多答案,但没有找到针对我的案例的解决方案。提前致谢!
<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
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
像这样在 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>
您可以按如下方式使用约束布局:
<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>
添加一个Drawable资源文件,用于为计数器添加背景如下:
<?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>
我想在 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 class:
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);
}
}
请帮助找到解决此问题的方法。我搜索了很多答案,但没有找到针对我的案例的解决方案。提前致谢!
<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
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
像这样在 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>
您可以按如下方式使用约束布局:
<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>
添加一个Drawable资源文件,用于为计数器添加背景如下:
<?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>