SupportActionBar:如何更改标题?
SupportActionBar: How to change the title?
此布局来自 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 清单中指定),因为它出现在 Activity header(应用程序栏、工具栏,无论它是什么)。到目前为止,我还无法做到这一点。
不,正如名称所示,您不能在清单中添加、删除或更改任何内容,它是一个清单文件,系统在安装您的应用程序时会读取该文件。
如果您只是想更改标题,您可以使用:
如果您正在使用 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,那么您可以使用 Toolbar。必须将工具栏添加到您的 XML。
此布局来自 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 清单中指定),因为它出现在 Activity header(应用程序栏、工具栏,无论它是什么)。到目前为止,我还无法做到这一点。
不,正如名称所示,您不能在清单中添加、删除或更改任何内容,它是一个清单文件,系统在安装您的应用程序时会读取该文件。
如果您只是想更改标题,您可以使用:
如果您正在使用 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,那么您可以使用 Toolbar。必须将工具栏添加到您的 XML。