Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中更改活动标题?_Android_Android Activity - Fatal编程技术网

如何在Android中更改活动标题?

如何在Android中更改活动标题?,android,android-activity,Android,Android Activity,我正在使用 Window w = getWindow(); w.setTitle("My title"); 更改我当前活动的标题,但它似乎不起作用 有人能告诉我如何更改此设置吗?请自行尝试设置标题,如下所示: setTitle("Hello StackOverflow"); 仅供参考,您可以选择从XML执行此操作 在AndroidManifest.xml中,可以使用 android:label="My Activity Title" 或 例如: <activity

我正在使用

Window w = getWindow();
w.setTitle("My title");
更改我当前活动的标题,但它似乎不起作用


有人能告诉我如何更改此设置吗?

请自行尝试设置标题,如下所示:

setTitle("Hello StackOverflow");

仅供参考,您可以选择从XML执行此操作

在AndroidManifest.xml中,可以使用

android:label="My Activity Title"

例如:

    <activity
        android:name=".Splash"
        android:label="@string/splash_activity_title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

如果您只需要一次,并让系统处理其余部分(非动态),则在清单文件中执行以下操作:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
            <intent-filter android:label="@string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

//这是我在活动中的自定义标题名 这对我有用

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment, container, false);
    getActivity().setTitle("My Title");
//...
}

如果要在Java文件中设置标题,请在activity onCreate中写入

setTitle("Your Title");
如果您想在清单中输入,请编写

    <activity
        android:name=".MainActivity"
        android:label="Your Title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

有一种更快的方法,只需使用

YourActivity.setTitle("New Title");
您还可以在onCreate()中找到它,例如:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("My Title");
    }

顺便说一句,您不能以静态方式调用setTitle(),而不传递任何活动对象。

如果您有多个活动,您可以在AndroidManifest.xml中这样设置它

<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=".NumbersActivity"
        android:label="@string/category_numbers"
        android:theme="@style/category_numbers" />
    <activity
        android:name=".FamilyActivity"
        android:label="@string/category_family"
        android:theme="@style/category_family" />
    <activity
        android:name=".ColorsActivity"
        android:label="@string/category_colors"
        android:theme="@style/category_colors" />
    <activity
        android:name=".PhrasesActivity"
        android:label="@string/category_phrases"
        android:theme="@style/category_phrases" />
    <activity
        android:name=".ExperimentActivity"
        android:label="@string/category_experiment"
        android:theme="@style/category_experiment" />
</application>


我的活动中有一个工具栏和一个覆盖所有标题的基本活动。因此,我必须在活动中使用onResume()中的setTitle,如下所示:

@Override
  protected void onResume() {
    super.onResume();
    toolbar.setTitle(R.string.title);
  }

如果要在更改活动时更改活动标题,请单击按钮。在MainActivity中声明必要的变量:

    private static final String TITLE_SIGN = "title_sign";
    ImageButton mAriesButton;
setTitle("Title Text");
public class act1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);

    setTitle("First Activity");

}
在onCreate()中添加onClickListener并为另一个活动创建新的意图:

    mTitleButton = (ImageButton) findViewById(R.id.title_button);
    mTitleButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, 
        SignActivity.class);
        String title_act = getText(R.string.simple_text).toString();
        intent.putExtra("title_act", title_act);
        startActivity(intent);
        finish();
        }
    });
onCreate()中的第二个活动代码:


我正在使用Android Studio 3.0.1

通过一项活动:

setTitle("Title Text");
在片段内部:

getActivity().setTitle("Title Text");

代码帮助我更改了标题

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    ActivityName.this.setTitle("Your Activity Title");}

如果使用的是OnCreateOptions菜单,还可以在OnCreateOptions菜单中添加代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    setTitle("Neue Aktivität");
    return true;
}
在主活动中:

    private static final String TITLE_SIGN = "title_sign";
    ImageButton mAriesButton;
setTitle("Title Text");
public class act1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);

    setTitle("First Activity");

}

}setTitle(“欢迎活动”)

你为什么要投反对票?很高兴知道你也可以从XML来做这件事。这几乎是做这件事的正确方法,通过引用外部字符串来实现轻松的本地化。应该是可接受的答案。它与setTitle()不同。设置启动器活动的android:label属性也会更改手机应用程序屏幕上的应用程序名称。您的应用程序图标将有“我的活动标题”标题。@doran虽然您是正确的,他的示例在某种程度上是错误的,但他也是正确的。他的问题是在启动屏幕上添加标签,而不是在启动屏幕上添加标签,而只是在启动屏幕上添加应用程序名称。但是,如果设置了启动器,则整个应用程序的标签决定了它的标题。这应该是最好的答案。setTitle不适用于我,getSupportActionBar()和getActionBar()也为空。我无法在运行时设置标题。@Ninja_编码,请尝试从活动调用它。或title=“Hello StackOverflow”在里面Kotlin@Sujay是的,您可以,在这里:尝试{int labelRes=getPackageManager().getActivityInfo(getComponentName(),0).labelRes;Logger.d(this.getClass().getSimpleName(),“labelRes:+labelRes”);if(labelRes>0&&getSupportActionBar()!=null){getSupportActionBar().setTitle(labelRes);}}catch(PackageManager.NameNotFoundException异常){Logger.d(this.getClass().getSimpleName(),“捕获异常:”+Log.getStackTraceString(异常));}当按下待机按钮并使屏幕再次激活时,这是不起作用的。标题不更新。有任何想法吗?这是因为在您的示例中不会调用OnCeReTE()。您应该考虑使用另一个事件处理程序,例如OnReSuMe()。。我建议您查看此链接:我完全是这样做的,它不起作用。在我看到的每个活动中,都只有应用程序名称。无法更改。现在已超过半个小时:(与2010年接受的答案相同的解决方案)