【问题标题】:adding an element to the label bar向标签栏添加元素
【发布时间】:2019-03-07 09:36:24
【问题描述】:

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

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

<activity android:name=".activities.application.Limiter" android:label="@string/LIMITER_ACTION_BAR_TITLE" />

【问题讨论】:

    标签: android label element add


    【解决方案1】:

    您可以在主活动布局中添加 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>
    

    【讨论】:

      【解决方案2】:

      我在一个新的 .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

      感谢大家的帮助,希望有人能发现这个有用!

      【讨论】:

        猜你喜欢
        • 2014-06-13
        • 1970-01-01
        • 2017-11-29
        • 2011-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-04
        • 1970-01-01
        相关资源
        最近更新 更多