在标签栏中添加元素

问题描述 投票:0回答:2

我想在右上角的标签栏中添加一个元素.element like the green square in the top right corner此元素的用途是通过更改颜色(绿色,黄色或红色)来指示某些应用状态。它应该持续通过所有的应用程序活动。如果有人对我将如何继续提出其他建议,我很乐意接受。唯一的条件是它处于图片中标记的位置。元素可以是一个图像(我将加载3个图像中的一个)或textview我只会改变背景颜色,或其他一些解决方案。

这是在AndroidManifest.xml中定义label的代码:

<activity android:name=".activities.application.Limiter" android:label="@string/LIMITER_ACTION_BAR_TITLE" />
android label element add
2个回答
0
投票

您可以在主活动布局中添加android toolbar小部件,下面的代码将给出您想要的结果

<android.support.v7.widget.Toolbar
    android:id="@+id/home_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/logoXmarks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/common_full_open_on_phone" />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

0
投票

我在新的.xml文件中创建了一个菜单项:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="40dp"
android:layout_height="40dp">
<item
    android:id="@+id/menu_item"
    android:icon="@drawable/circle_ok"
    app:showAsAction="always"
    android:title=""/>
</menu>

然后将此代码添加到活动:

private MenuItem menuItem;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_item, menu);
    menuItem = menu.findItem(R.id.menu_item);
    return true;
}

更改我使用的图标:

public boolean onPrepareOptionsMenu(Menu menu) {

    switch(status) {
        case 0:
            menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_ok);
            break;
        case 1:
            menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_warning);
            break;
        case 2:
            menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_alarm);
            break;
    }

    return super.onPrepareOptionsMenu(menu);
}

更改状态值后invalidateOptionsMenu();应该叫。这是结果:color changes every second

大家帮帮忙,希望有人能发现这个有用!

© www.soinside.com 2019 - 2024. All rights reserved.