目前它只显示应用程序的名称,我希望它显示一些自定义的东西,在我的应用程序中的每个屏幕都是不同的。

例如:我的主屏幕可以在操作栏中显示“page1”,而应用程序切换到的另一个活动可以在该屏幕操作栏中显示“page2”。


当前回答

只需在onCreate方法中添加此代码。

setTitle("new title");

其他回答

年纪大一点,但也有同样的问题。我是这样做的:

strings.xml

<字符串

确保你在AndroidManifest.xml中设置了这个:

<activity
            ...
            android:label="@string/title_awesome_app" >
            ...
</activity>

这很简单,你不必担心空引用和其他东西。

科特林

您可以以这种方式在Kotlin中以编程方式完成它。如果"supportActionBar"为空,你就忽略它:

    supportActionBar?.setTitle(R.string.my_text)
    supportActionBar?.title = "My text"

或者这样。如果“supportActionBar”为空,则抛出异常。

    supportActionBar!!.setTitle(R.string.my_text)
    supportActionBar!!.title = "My text"

如果你正在使用导航栏来改变片段,那么你可以在你正在改变片段的地方添加更改,如下所示:

 public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.clientsidedrawer:
                    //    Toast.makeText(getApplicationContext(),"Client selected",Toast.LENGTH_SHORT).show();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new clients_fragment()).commit();
                    break;
    
                case R.id.adddatasidedrawer:
                    getSupportActionBar().setTitle("Add Client");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addclient_fragment()).commit();
                    break;
    
                case R.id.editid:
                    getSupportActionBar().setTitle("Edit Clients");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Editclient()).commit();
                    break;
    
                case R.id.taskid:
                    getSupportActionBar().setTitle("Task manager");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Taskmanager()).commit();
                    break;

如果你正在使用简单的活动,那么只需调用:

getSupportActionBar().setTitle("Contact Us");

更改动作栏/工具栏的颜色使用:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#06023b")));

要将渐变设置为动作栏,首先创建渐变: 创建示例:> R.drawable.gradient_contactus

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >

    <gradient
        android:angle="90"
        android:startColor="#2980b9"
        android:centerColor="#6dd5fa"
        android:endColor="#2980b9">
    </gradient>


</shape>

然后像这样设置:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));

下面的代码在Oncreate方法中工作得很好

    getSupportActionBar().setTitle("Text to display");

我们可以用以下两种方式之一更改动作栏标题:

In the Manifest: in the manifest file, set the label of each Activity. android:label="@string/TitleWhichYouWantToDisplay" In code: in code, call the setTitle() method with a String or the id of String as the argument. public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { setTitle(R.string.TitleWhichYouWantToDisplay); // OR You can also use the line below // setTitle("MyTitle") setContentView(R.layout.activity_main); } }