Setting Action Bar title and subtitle

后端 未结 12 695
盖世英雄少女心
盖世英雄少女心 2020-12-01 02:41

I want to set title and subtitile of my action bar before compile time. I got a way to do it like this:

ActionBar ab = getActionBar();
ab.setTitle(\"My Title         


        
相关标签:
12条回答
  • 2020-12-01 03:10

    There is another option that no one commented, from the Support Library V7 was implemented by Google ActionBarSupport, which is compatible ActionBar from Android 2.2 to the last.
    If you change your ActionBar this you can do getSupportActionBar().setTitle("") on all Android versions.

    0 讨论(0)
  • 2020-12-01 03:13

    Try this one:

    android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setTitle("This is Title");
    ab.setSubtitle("This is Subtitle");
    
    0 讨论(0)
  • 2020-12-01 03:14

    Nope! You have to do it at runtime.

    If you want to make your app backwards compatible, then simply check the device API level. If it is higher than 11, then you can fiddle with the ActionBar and set the subtitle.

    if(Integer.valueOf(android.os.Build.VERSION.SDK) >= 11){
        //set actionbar title
    }
    
    0 讨论(0)
  • 2020-12-01 03:15

    You can do something like this to code for both versions:

    /**
     * Sets the Action Bar for new Android versions.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void actionBarSetup() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar ab = getActionBar();
        ab.setTitle("My Title");
        ab.setSubtitle("sub-title"); 
      }
    }
    

    Then call actionBarSetup() in onCreate(). The if runs the code only on new Android versions and the @TargetApi allows the code to compile. Therefore it makes it safe for both old and new API versions.

    Alternatively, you can also use ActionBarSherlock (see edit) so you can have the ActionBar on all versions. You will have to do some changes such as making your Activities extend SherlockActivity and calling getSupportActionBar(), however, it is a very good global solution.

    Edit

    Note that when this answer was originally written, ActionBarSherlock, which has since been deprecated, was the go-to compatibility solution.

    Nowadays, Google's appcompat-v7 library provides the same functionality but is supported (and actively updated) by Google. Activities wanting to implement an ActionBar must:

    • extend AppCompatActivity
    • use a Theme.AppCompat derivative

    To get an ActionBar instance using this library, the aptly-named getSupportActionBar() method is used.

    0 讨论(0)
  • 2020-12-01 03:21

    Try This

    Just go to your Manifest file. and You have define the label for each activity in your manifest file.

    <activity
            android:name=".Search_Video"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
        </activity>
    

    here change

    android:label="@string/your_title"

    0 讨论(0)
  • 2020-12-01 03:24

    The title for the actionbar could be in the AndroidManifest, here:

    <activity 
        . . .
              android:label="string resource" 
        . . .
    </activity>
    

    android:label A user-readable label for the activity. The label is displayed on-screen when the activity must be represented to the user. It's often displayed along with the activity icon. If this attribute is not set, the label set for the application as a whole is used instead (see the element's label attribute). The activity's label — whether set here or by the element — is also the default label for all the activity's intent filters (see the element's label attribute). The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface. However, as a convenience while you're developing the application, it can also be set as a raw string.

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