【问题标题】:How to change title of Activity in Android?如何在 Android 中更改 Activity 的标题?
【发布时间】:2011-01-12 23:51:39
【问题描述】:

我正在使用

Window w = getWindow();
w.setTitle("My title");

更改我当前活动的标题,但它似乎不起作用。

谁能指导我如何改变这个?

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    单独尝试 setTitle,如下所示:

    setTitle("Hello StackOverflow");
    

    【讨论】:

    • setTitle 对我不起作用,getSupportActionBar() 和 getActionBar() 也无效,我无法在运行时设置标题。
    • @Ninja_Coding,尝试从 Activity 调用它。
    • 或在 Kotlin 中 title = "Hello StackOverflow"
    【解决方案2】:

    仅供参考,您可以选择从 XML 中执行。

    在AndroidManifest.xml中,可以用

    android:label="My Activity Title"
    

    或者

    android:label="@string/my_activity_label"
    

    例子:

        <activity
            android:name=".Splash"
            android:label="@string/splash_activity_title" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

    【讨论】:

    • 你为什么要投反对票?很高兴知道您也可以从 XML 中做到这一点。
    • 这几乎是正确的方法,通过引用外部字符串来实现轻松本地化。应该被接受的答案。
    • 和 setTitle() 不一样。设置启动器活动的 android:label 属性也会更改手机应用程序屏幕上的应用程序名称。您的应用程序图标将带有“我的活动标题”标题。
    • @doran 虽然你是对的,他的例子在某种程度上是错误的,但他也是对的。他的问题是为启动画面添加标签,而您不会为启动画面这样做,而只需使用应用程序名称标记它。然而,整个应用程序的标签决定了启动器的标题(如果已设置)
    • 这应该是最好的答案了。
    【解决方案3】:

    如果您需要一次并让系统处理其余部分(非动态),请在您的清单文件中执行以下操作:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
                <intent-filter android:label="@string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    

    【讨论】:

      【解决方案4】:
      setTitle(getResources().getText(R.string.MyTitle));
      

      【讨论】:

      • @Sujay 是的,你可以,你在这里:尝试 { int labelRes = getPackageManager().getActivityInfo(getComponentName(), 0).labelRes; Logger.d(this.getClass().getSimpleName(), "labelRes : " + labelRes); if (labelRes > 0 && getSupportActionBar() != null) { getSupportActionBar().setTitle(labelRes); } } catch (PackageManager.NameNotFoundException 异常) { Logger.d(this.getClass().getSimpleName(), "异常捕获:" + Log.getStackTraceString(exception)); }
      【解决方案5】:

      有一个更快的方法,只需使用

      YourActivity.setTitle("New Title");
      

      您也可以在 onCreate() 中找到它,例如:

      public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              this.setTitle("My Title");
          }
      

      顺便说一句,你不能做的是在不传递任何 Activity 对象的情况下以静态方式调用 setTitle()。

      【讨论】:

      • 这在按下待机按钮并再次激活屏幕时不起作用。标题不更新。有什么想法吗?
      • 那是因为 onCreate() 在您的示例中不会被调用。您应该考虑使用另一个事件处理程序,例如 onResume()。我建议你看这个链接:developer.android.com/guide/components/activities/…
      【解决方案6】:

      这对我有用。

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment, container, false);
          getActivity().setTitle("My Title");
      //...
      }
      

      【讨论】:

        【解决方案7】:

        如果你有多个activity,你可以在AndroidManifest.xml中这样设置

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
        
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".NumbersActivity"
                android:label="@string/category_numbers"
                android:theme="@style/category_numbers" />
            <activity
                android:name=".FamilyActivity"
                android:label="@string/category_family"
                android:theme="@style/category_family" />
            <activity
                android:name=".ColorsActivity"
                android:label="@string/category_colors"
                android:theme="@style/category_colors" />
            <activity
                android:name=".PhrasesActivity"
                android:label="@string/category_phrases"
                android:theme="@style/category_phrases" />
            <activity
                android:name=".ExperimentActivity"
                android:label="@string/category_experiment"
                android:theme="@style/category_experiment" />
        </application>
        

        【讨论】:

        • 我就是这样做的,但是没有用。在每项活动中,我只看到应用程序名称。无法改变它。现在已经半个多小时了:(
        【解决方案8】:

        我使用的是 Android Studio 3.0.1。

        有一个活动:

        setTitle("Title Text");
        

        片段内部:

        getActivity().setTitle("Title Text");
        

        【讨论】:

          【解决方案9】:
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.Main_Activity);
              this.setTitle("Title name");
          }
          

          【讨论】:

            【解决方案10】:

            如果您想在 Java 文件中设置标题,请在您的活动中写入 onCreate

            setTitle("Your Title");
            

            如果你想在 Manifest 中写

                <activity
                    android:name=".MainActivity"
                    android:label="Your Title" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
            
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
            

            【讨论】:

              【解决方案11】:

              我的活动中有一个工具栏和一个覆盖所有标题的基本活动。所以我不得不在 Activity 的 onResume() 中使用 setTitle,如下所示:

              @Override
                protected void onResume() {
                  super.onResume();
                  toolbar.setTitle(R.string.title);
                }
              

              【讨论】:

                【解决方案12】:

                代码帮我改了标题。

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_name);
                    ActivityName.this.setTitle("Your Activity Title");}
                

                【讨论】:

                  【解决方案13】:

                  在 MainActivity 中:

                  public class act1 extends AppCompatActivity {
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.act1);
                  
                      setTitle("First Activity");
                  
                  }
                  

                  }

                  【讨论】:

                    【解决方案14】:

                    setTitle("任何应用程序"); 在 MainActivity.java 中更简单 我会说。

                    【讨论】:

                      【解决方案15】:

                      如果您想在单击按钮更改活动时更改活动标题。在 MainActivity 中声明必要的变量:

                          private static final String TITLE_SIGN = "title_sign";
                          ImageButton mAriesButton;
                      

                      在 onCreate() 中添加 onClickListener 并为另一个活动创建新意图:

                          mTitleButton = (ImageButton) findViewById(R.id.title_button);
                          mTitleButton.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
                              Intent intent = new Intent(MainActivity.this, 
                              SignActivity.class);
                              String title_act = getText(R.string.simple_text).toString();
                              intent.putExtra("title_act", title_act);
                              startActivity(intent);
                              finish();
                              }
                          });
                      

                      onCreate()中的SecondActivity代码:

                          String txtTitle = getIntent().getStringExtra("title_act");
                          this.setTitle(txtTitle);
                      

                      【讨论】:

                        【解决方案16】:

                        如果您使用 onCreateOptionsMenu,您还可以在 onCreateOptionsMenu 中添加 setTitle 代码。

                        @Override
                        public boolean onCreateOptionsMenu(Menu menu) {
                            MenuInflater inflater = getMenuInflater();
                            inflater.inflate(R.menu.menu, menu);
                        
                            setTitle("Neue Aktivität");
                            return true;
                        }
                        

                        【讨论】:

                          【解决方案17】:

                          setTitle("欢迎活动");

                          【讨论】:

                          • 与 2010 年接受的答案相同的解决方案
                          【解决方案18】:

                          在 Kotlin 中,这样:

                          this.title = resources.getText(R.string.fast_learning)
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 2020-06-23
                            • 1970-01-01
                            • 1970-01-01
                            • 2015-10-21
                            • 1970-01-01
                            • 1970-01-01
                            • 2018-12-22
                            相关资源
                            最近更新 更多