如何用代码更改屏幕左上角标题栏的标题

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

我使用底部导航活动模板创建一个android项目,然后得到三个默认片段,

在方法中:DashboardFragment的onCreateView,我添加了getActivity()。setTitle(“ Important”);

但是标题(在屏幕的左上角)仍然是“仪表板”,为什么没有更改为“重要”?我想用代码更改标题,我想在运行应用程序时更改标题,而不仅仅是在mobile_navigation.xml中进行配置]

谢谢。

这里是mobile_navigation.xml这是mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.testfragmentjump.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.testfragmentjump.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.example.testfragmentjump.ui.notifications.NotificationsFragment"
        android:laabel="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>
android
1个回答
0
投票
首先,您必须删除Dashboard Fragment的标题以检查它在XML或Code类中的位置,然后通过代码根据您选择的片段来更改标题!
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.