【问题标题】:How to display countdown timer on status bar in android, like time display?如何在android的状态栏上显示倒数计时器,如时间显示?
【发布时间】:2017-01-15 13:36:28
【问题描述】:

我正在制作一个允许用户选择日期和时间的应用程序。到达日期和时间后,屏幕上将显示动画。在时间之前,它只会在屏幕上显示倒计时时间(如 xxx:xx:xx)。我使用每秒变化的文本视图实现了这一点。如何在状态栏上显示此文本,例如显示时间

我尝试过使用 Notification 构造函数,就像在我在这里找到的一些帖子中一样,但它们已被贬低并且不再起作用。 我尝试使用 NotificationCompat.Builder,但它确实没有办法设置可以显示在状态栏上的文本。

任何人都知道任何工作arpund解决方案,请给我一个答案。我只需要在图标的状态栏上显示一个文本

【问题讨论】:

  • 你试过NotificationCompat.Builder类中的setTicker方法吗?
  • 我试过了。问题是,当我的通知文本每秒更新一次时,setTicker 每秒都会在通知栏上显示它并带有动画(来自下方),这对用户来说非常烦人
  • 查看@Shazeel Afzal 的回答here。可能符合您正在寻找的内容
  • 创建通知并使用 setSmallIcon(resId, level)。用计数器更新级别。当然 resId 必须是可绘制的级别列表,并且您必须将所有数字作为可绘制文件夹中的图像
  • 我认为@uguboz 是正确的

标签: android android-layout android-notifications countdowntimer android-statusbar


【解决方案1】:

您可以通过传递布局文件或视图在 ActionBar 上设置 CustomView。这是一个示例代码。

protected void setCustomActionBarTitle(String title) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.layout_action_bar_title);
            TextView titleView = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
            titleView.setText(title);
            ImageView imageView = (ImageView) actionBar.getCustomView().findViewById(R.id.up_icon);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
    } 

【讨论】:

    【解决方案2】:

    将此添加到您的 activity_main xml 文件中

    <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:minHeight="@dimen/header_size"
                android:background="?attr/colorPrimary"
                android:layout_gravity="center"
                android:gravity="center">
                <TextView
                    android:id="@+id/title"
                    android:textColor="@color/title"
                    android:gravity="center"
                    android:background="@color/white"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/header_size"
                    android:textSize="@dimen/header_text_size"
                    android:textStyle="bold"
                    android:text=""/>
    
            </android.support.v7.widget.Toolbar>
    

    并在您的活动中添加这些行

        actionbar = getSupportActionBar();
        actionbar.setHomeAsUpIndicator(R.drawable.ic_back);
        actionbar.setDisplayHomeAsUpEnabled(true);
        title = (TextView) findViewById(R.id.title);
    

    现在你可以在这个textview中设置倒计时,也可以根据需要对齐TextView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2017-04-21
      相关资源
      最近更新 更多