android开发分享如何更改Android中Activity的标题?

我在用

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

改变我目前的活动标题,但似乎没有工作。

任何人都可以指导我如何改变这个?

    尝试setTitle本身,就像这样:

     setTitle("Hello StackOverflow"); 

    只是一个FYI,你可以select从XML。

    在AndroidManifest.xml中,可以使用

     android:label="My Activity Title" 

    要么

     android:label="@strings/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> 

    如果你想要一次,让系统处理其余的(不是dynamic的),那么在你的清单文件中这样做:

     <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> 

     setTitle(getResources().getText(R.string.MyTitle)); 

    这对我有效。

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

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

     YourActivity.setTitle("New Title"); 

    你也可以在onCreate()里面find它,例如:

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

    顺便说一句,你根本无法做的就是以静态方式调用setTitle()而不传递任何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> 

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

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

    如果您想在更改活动时通过单击button来更改活动的标题。 在MainActivity中声明必要的variables:

      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); 

    如果你想在Java文件中设置标题,那么写你的活动onCreate

     setTitle("Your Title"); 

    如果你想在清单然后写

      <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> 

      以上就是android开发分享如何更改Android中Activity的标题?相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516904.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐