设置操作栏标题和副标题

时间:2022-12-04 23:44:05

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");
ab.setSubtitle("sub-title");

But I don't want to do it on run time. Is there any xml file or any location where I can specify these titles?

但我不想在运行时这样做。是否有任何xml文件或任何我可以指定这些标题的位置?

I am trying to achieve this:

我想要实现这个目标:

设置操作栏标题和副标题

Edit:

The reason why I want it in xml is that I want my app to be supported in API level 8. And the method getActionBar() is supported at least on level 11.

我想在xml中使用它的原因是我希望我的应用程序在API级别8中受支持。并且至少在级别11上支持方法getActionBar()。

11 个解决方案

#1


82  

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.

然后在onCreate()中调用actionBarSetup()。 if仅在新的Android版本上运行代码,而@TargetApi允许代码编译。因此,它使旧版和新版API都安全。

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.

或者,您也可以使用ActionBarSherlock(请参阅编辑),以便您可以在所有版本上使用ActionBar。您将不得不做一些更改,例如让您的活动扩展SherlockActivity并调用getSupportActionBar(),但是,它是一个非常好的全局解决方案。

Edit

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

请注意,在最初编写此答案时,ActionBarSherlock(已被弃用)是首选兼容性解决方案。

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:

如今,谷歌的appcompat-v7库提供相同的功能,但谷歌支持(并积极更新)。想要实现ActionBar的活动必须:

  • extend AppCompatActivity
  • 扩展AppCompatActivity
  • use a Theme.AppCompat derivative
  • 使用Theme.AppCompat派生物

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

要使用此库获取ActionBar实例,请使用名称恰当的getSupportActionBar()方法。

#2


14  

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

操作栏的标题可以在AndroidManifest中,在这里:

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

android:label活动的用户可读标签。当必须向用户表示活动时,标签显示在屏幕上。它通常与活动图标一起显示。如果未设置此属性,则使用整个应用程序的标签集(请参阅元素的label属性)。活动的标签 - 无论是在此处还是由元素设置 - 也是所有活动的intent过滤器的默认标签(请参阅元素的label属性)。应将标签设置为对字符串资源的引用,以便它可以像用户界面中的其他字符串一样进行本地化。但是,为了方便您开发应用程序,它也可以设置为原始字符串。

#3


8  

Try this one:

试试这个:

android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle("This is Title");
ab.setSubtitle("This is Subtitle");

#4


5  

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.

还有另一个选项,没有人评论,支持库V7是由Google ActionBarSupport实现的,它是从Android 2.2到最后一个兼容的ActionBar。如果您更改了ActionBar,则可以在所有Android版本上执行getSupportActionBar()。setTitle(“”)。

#5


4  

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

将此代码放入您的Android清单文件中,它应该将操作栏的标题设置为您想要的!

#6


3  

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.

如果您想使您的应用向后兼容,那么只需检查设备API级别即可。如果它高于11,那么你可以摆弄ActionBar并设置字幕。

if(Integer.valueOf(android.os.Build.VERSION.SDK) >= 11){
    //set actionbar title
}

#7


1  

Try This:

尝试这个:

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

在strings.xml中添加你的标题和副标题......

ActionBar ab = getActionBar();
ab.setTitle(getResources().getString(R.string.myTitle));
ab.setSubtitle(getResources().getString(R.string.mySubTitle));

#8


0  

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.

这样,您只需要指定一次标题字符串,然后就可以在其旁边指定字幕。

#9


0  

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

您可以使用AndroidManifest.xml在操作栏中设置标题。向活动添加标签

<activity
            android:name=".YourActivity"
            android:label="Your Title" />

#10


0  

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"

机器人:标签= “@字符串/ your_title”

#11


0  

This is trivial one-liner in Kotlin

这在Kotlin中是微不足道的单行

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

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

#1


82  

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.

然后在onCreate()中调用actionBarSetup()。 if仅在新的Android版本上运行代码,而@TargetApi允许代码编译。因此,它使旧版和新版API都安全。

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.

或者,您也可以使用ActionBarSherlock(请参阅编辑),以便您可以在所有版本上使用ActionBar。您将不得不做一些更改,例如让您的活动扩展SherlockActivity并调用getSupportActionBar(),但是,它是一个非常好的全局解决方案。

Edit

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

请注意,在最初编写此答案时,ActionBarSherlock(已被弃用)是首选兼容性解决方案。

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:

如今,谷歌的appcompat-v7库提供相同的功能,但谷歌支持(并积极更新)。想要实现ActionBar的活动必须:

  • extend AppCompatActivity
  • 扩展AppCompatActivity
  • use a Theme.AppCompat derivative
  • 使用Theme.AppCompat派生物

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

要使用此库获取ActionBar实例,请使用名称恰当的getSupportActionBar()方法。

#2


14  

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

操作栏的标题可以在AndroidManifest中,在这里:

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

android:label活动的用户可读标签。当必须向用户表示活动时,标签显示在屏幕上。它通常与活动图标一起显示。如果未设置此属性,则使用整个应用程序的标签集(请参阅元素的label属性)。活动的标签 - 无论是在此处还是由元素设置 - 也是所有活动的intent过滤器的默认标签(请参阅元素的label属性)。应将标签设置为对字符串资源的引用,以便它可以像用户界面中的其他字符串一样进行本地化。但是,为了方便您开发应用程序,它也可以设置为原始字符串。

#3


8  

Try this one:

试试这个:

android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle("This is Title");
ab.setSubtitle("This is Subtitle");

#4


5  

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.

还有另一个选项,没有人评论,支持库V7是由Google ActionBarSupport实现的,它是从Android 2.2到最后一个兼容的ActionBar。如果您更改了ActionBar,则可以在所有Android版本上执行getSupportActionBar()。setTitle(“”)。

#5


4  

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

将此代码放入您的Android清单文件中,它应该将操作栏的标题设置为您想要的!

#6


3  

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.

如果您想使您的应用向后兼容,那么只需检查设备API级别即可。如果它高于11,那么你可以摆弄ActionBar并设置字幕。

if(Integer.valueOf(android.os.Build.VERSION.SDK) >= 11){
    //set actionbar title
}

#7


1  

Try This:

尝试这个:

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

在strings.xml中添加你的标题和副标题......

ActionBar ab = getActionBar();
ab.setTitle(getResources().getString(R.string.myTitle));
ab.setSubtitle(getResources().getString(R.string.mySubTitle));

#8


0  

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.

这样,您只需要指定一次标题字符串,然后就可以在其旁边指定字幕。

#9


0  

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

您可以使用AndroidManifest.xml在操作栏中设置标题。向活动添加标签

<activity
            android:name=".YourActivity"
            android:label="Your Title" />

#10


0  

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"

机器人:标签= “@字符串/ your_title”

#11


0  

This is trivial one-liner in Kotlin

这在Kotlin中是微不足道的单行

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

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