Setting Action Bar title and subtitle

后端 未结 12 694
盖世英雄少女心
盖世英雄少女心 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:01

    You can set the title in action-bar using AndroidManifest.xml. Add label to the activity

    <activity
                android:name=".YourActivity"
                android:label="Your Title" />
    
    0 讨论(0)
  • 2020-12-01 03:03
    supportActionBar?.title = "Hola tio"
    supportActionBar?.subtitle = "Vamos colega!"
    
    0 讨论(0)
  • 2020-12-01 03:04
        <activity android:name=".yourActivity" android:label="@string/yourText" />
    

    Put this code into your android manifest file and it should set the title of the action bar to what ever you want!

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

    Try This:

    In strings.xml add your title and subtitle...

    ActionBar ab = getActionBar();
    ab.setTitle(getResources().getString(R.string.myTitle));
    ab.setSubtitle(getResources().getString(R.string.mySubTitle));
    
    0 讨论(0)
  • 2020-12-01 03:05

    For an activity you can use this approach to specify a subtitle, along with the title, in the manifest.

    Manifest:

    <activity
        android:name=".MyActivity"
        android:label="@string/my_title"
        android:description="@string/my_subtitle">
    </activity>
    

    Activity:

    try {
        ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
        //String title = activityInfo.loadLabel(getPackageManager()).toString();
        int descriptionResId = activityInfo.descriptionRes;
        if (descriptionResId != 0) {
            toolbar.setSubtitle(Utilities.fromHtml(getString(descriptionResId)));
        }
    }
    catch(Exception e) {
        Log.e(LOG_TAG, "Could not get description/subtitle from manifest", e);
    }
    

    This way you only need to specify the title string once, and you get to specify the subtitle right alongside it.

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

    This is trivial one-liner in Kotlin

    supportActionBar?.title = getString(R.string.coolTitle)

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