【问题标题】:SupportActionBar: How to change the title?SupportActionBar:如何更改标题?
【发布时间】:2019-11-05 21:20:09
【问题描述】:

此布局来自 Studio 3.5.1 模板。我想不通。但我的问题希望有一个简单的答案。

这是 XML 布局:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ChecklistActivity">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabAppBarLayout"
            android:theme="@style/AppTheme.AppBarOverlay">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayoutView"
                app:tabMode="scrollable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary" />

        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

在我的应用程序 Activity (Java) 中,我想更改 android:label="@string/app_name 文本(在 Activity Manifest 中指定),因为它出现在 Activity 标题(appBar、toolBar 等)上。到目前为止,我还无法做到这一点。

【问题讨论】:

  • 我想在该标签中放置一个计时器“(hhh:mm)”,每分钟更新一次。我知道如何编写计时器代码。这不合适吗?更好的选择?
  • 感谢您输入 Richard,“为什么”我要更改该标签是否重要?对于 Manifest 中定义的每个活动,该标签都不同。我想根据某些事件更新该标签。在一种情况下,将该活动的经过时间(以小时和分钟为单位)附加到标签上。谢谢
  • 为什么是因为我不太明白这个问题,只是想了解更多上下文。
  • 我已经更新了我的问题,以便更清楚地了解我想要更改的内容。我不想根据我显示的 XML 布局文件更改清单,只是在我的活动中显示的那个标签。我不知道如何用我的布局做到这一点。

标签: android appbar


【解决方案1】:

不,您不能在清单中添加、删除或更改任何内容,顾名思义,它是一个清单文件,系统在安装您的应用程序时会读取该文件。

如果你只是想改变你可以使用的标题:

如果您使用的是ActionBar

getActionBar().setTitle(int titleId)

如果您正在扩展AppCompatActivity

getSupportActionBar().setTitle(int titleId)

例如:

public class ChecklistActivity extends AppCompatActivity {

    @Override 
    public void onCreate(...) {
        doMyTimerThing();
    }

    public void doMyTimerThing() {
       onTick(long timeLeft) {
           ...(long to string here)...
           getSupportActionBar().setTitle(timeToShowOnActionBar)
       }
    }
}

Set Up the app bar Android Dev Guide.

ActionBar vs SupportActionBar

ActionBar Docs

这是一个简单的实现,如果您想在应用程序的进展过程中对 ActionBar 有更多的控制权,那么您可以使用Toolbar。必须将工具栏添加到您的 XML 中。

【讨论】:

  • 好的 - 如果这是真的,那么问题得到了回答。我需要专门为此任务在布局中添加一个小部件。我不知道。谢谢
  • 好的,我想我明白了。
  • 更新了我的答案,试图更清楚。您不需要更改清单中的标签,也不能只是尝试在运行时更改标题。
  • 嘿,如果您想在活动中更改操作栏标题名称,请仅使用 getSupportActionBar().setTitle("title");
  • 就是这样,伙计们!这就是我所追求的。对不起,我没有在我的问题中很好地解释这一切。显然,这对我来说是个谜的部分原因。我宁愿摆脱 coordinatorlayout 并使用一些更明确的东西,比如约束布局。我的下一个任务就是将该 XML 布局转换为约束布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
相关资源
最近更新 更多