How to change the text on the action bar

前端 未结 18 747
春和景丽
春和景丽 2020-11-22 12:30

Currently it just displays the name of the application and I want it to display something custom and be different for each screen in my app.

For example: my home scr

相关标签:
18条回答
  • 2020-11-22 13:15

    The best way to change the action bar name is to go to the AndroidManifest.xml and type this code.

    <activity android:name=".YourActivity"
            android:label="NameYouWantToDisplay> </activity>
    
    0 讨论(0)
  • 2020-11-22 13:19

    We can change the ActionBar title in one of two ways:

    1. In the Manifest: in the manifest file, set the label of each Activity.

      android:label="@string/TitleWhichYouWantToDisplay"
      
    2. In code: in code, call the setTitle() method with a String or the id of String as the argument.

      public class MainActivity extends Activity {
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
      
              setTitle(R.string.TitleWhichYouWantToDisplay);
              // OR You can also use the line below
              // setTitle("MyTitle")
              setContentView(R.layout.activity_main);
      
          }
      }
      
    0 讨论(0)
  • 2020-11-22 13:23

    You can define your title programatically using setTitle within your Activity, this method can accept either a String or an ID defined in your values/strings.xml file. Example:

    public class YourActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            setTitle(R.string.your_title);
            setContentView(R.layout.main);
    
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:23

    The easiest way is to call this.setTitle("...") if you are in the activity. And if you are in a fragment, just call getActivity().setTitle("...");

    This way will let you change the title anytime, no need to call it before setContentView(R.layout.activity_test);

    0 讨论(0)
  • 2020-11-22 13:24
    getActionBar().setTitle("edit your text"); 
    
    0 讨论(0)
  • 2020-11-22 13:24

    getSupportActionBar().setTitle("title");

    0 讨论(0)
提交回复
热议问题