用BadgeView在actionbar menu上顯示提醒信息

有時候須要在actionbar menu上顯示消息個數,因此能夠用BadgeView來實現。 android

實現方法是獲取actionbar menuitemview,而後建立BadgeView。注意,一般只能在Optionsmenu建立完成以後去獲取menuitemview。咱們能夠在onCreate方法中延時1秒去處理。 app

<menu xmlns:android="http://schemas.android.com/apk/res/android" ide

    xmlns:app="http://schemas.android.com/apk/res-auto" post

    xmlns:tools="http://schemas.android.com/tools" ui

    tools:context="com.example.demo.MainActivity" > this

    <item spa

        android:id="@+id/action_settings" xml

        android:orderInCategory="100" string

        android:title="@string/action_settings" it

        app:showAsAction="always"/>

 

</menu>

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

new Handler().postDelayed(new Runnable() {

 

@Override

public void run() {

View view = findViewById(R.id.action_setting);

badgeview = new BadgeView(MainActivity.this, view);

badgeview.setText("1");

badgeview.show();

}

}, 1000);

}

效果圖:

相關文章
相關標籤/搜索