简体   繁体   English

Android Activity操作栏标签设置不正确?

[英]Android Activity action bar label not being set properly?

I have an app I'm designing where I have an activity with several buttons in it. 我有一个正在设计的应用程序,其中有一个带有多个按钮的活动。 This activity uses "setTitle()" to set the action bar title to the name of the user, where the default was set to a blank string in the AndroidManifest via android:label. 此活动使用“ setTitle()”将操作栏标题设置为用户名,其中默认值通过android:label在AndroidManifest中设置为空白字符串。 One of the buttons leads to another activity that has a tab layout with fragments with different pieces of data. 其中一个按钮导致另一个活动,该活动的选项卡布局带有带有不同数据片段的片段。 I use android:label in the manifest to set the title of this tab layout activity to a raw string. 我在清单中使用android:label将此标签布局活动的标题设置为原始字符串。 However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. 但是,在输入此活动时,它会在启动该活动的活动标题中显示用户名。 Why is this? 为什么是这样? Is there something wrong with how I'm defining the label of the activities? 我如何定义活动标签有问题吗? Here is my AndroidManifest: 这是我的AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.uml.android.adventurersarchive">

    <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=".Preferences"
                  android:parentActivityName=".MainActivity"
                  android:theme="@android:style/Theme.Holo" />
        <activity android:name=".CreateCharacterActivity"
                  android:parentActivityName=".MainActivity"
                  android:label="Create a Character" />
        <activity android:name=".CharacterMainActivity"
                  android:parentActivityName=".MainActivity"
                  android:label="" />
        <activity android:name=".CharacterSheetActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="" />
        <activity android:name=".CharacterEquipmentActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="Equipment" />
        <activity android:name=".CharacterSpellbookActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="Spellbook" />
    </application>
</manifest>

The activity in question which is displaying the wrong title is "CharacterSpellbookActivity". 标题显示错误的活动是“ CharacterSpellbookActivity”。 The one that sets the title to the user name is "CharacterMainActivity". 将标题设置为用户名的是“ CharacterMainActivity”。

Here is the onCreate() method of CharacterMainActivity: 这是CharacterMainActivity的onCreate()方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_main);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());
}

Here is where I launch the CharacterSpellbookActivity: 这是我启动CharacterSpellbookActivity的地方:

public void openSpellbook(View v) {
    Intent intent = new Intent(this, CharacterSpellbookActivity.class);
    intent.putExtra("character", myCharacter);
    startActivity(intent);
}

And here is the onCreate() method of the CharacterSpellbookActivity: 这是CharacterSpellbookActivity的onCreate()方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_spellbook);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    SpellbookTabAdapter adapter = new SpellbookTabAdapter(myCharacter, getSupportFragmentManager());

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

Did I do something wrong here? 我在这里做错了吗?

However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. 但是,在输入此活动时,它会在启动该活动的活动标题中显示用户名。 Why is this? 为什么是这样?

Because that's what you told it to do 因为那是你告诉它要做的

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_spellbook);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());

Empty labels automatically picks up their value if they are null from their parent Activity. 如果空标签在其父活动中为空,则会自动获取其值。

Lets solve this : 让我们解决这个问题:

remove the label attribute from manifest 从清单中删除标签属性

If you want to change only the title why don't you ease down on your code and try this: 如果您只想更改标题,为什么不放松代码并尝试以下操作:

1 1个

getActionBar().setTitle(R.string.your_title);

After it, you can call: 之后,您可以致电:

2 2

getActionBar().setDisplayShowTitleEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM