【问题标题】:How to change the text on the action bar如何更改操作栏上的文本
【发布时间】:2011-03-27 04:20:15
【问题描述】:

目前它只显示应用程序的名称,我希望它显示自定义的内容,并且在我的应用程序中的每个屏幕上都不同。

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

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    下面的代码在 Oncreate 方法中对我很有效

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

    【讨论】:

      【解决方案2】:

      在 onCreateView 中添加:

      (activity as AppCompatActivity).supportActionBar?.title ="My APP Title"
      

      【讨论】:

        【解决方案3】:

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

         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));
        

        【讨论】:

          【解决方案4】:

          科特林

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

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

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

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

          【讨论】:

          • 唯一真正的解决方案!这个答案不仅是最新的,而且还在使用 Kotlin。谢谢!
          【解决方案5】:

          getSupportActionBar().setTitle("title");

          【讨论】:

          • 纯代码答案是低质量的答案。添加一些解释为什么会这样。
          【解决方案6】:

          适用于使用 AndroidX导航架构组件的未来开发者。

          而不是使用上述解决方案之一设置工具栏标题,这可能非常痛苦,如果您想在返回堆栈更改时动态设置它,您可以在导航图中为片段的标题设置一个占位符,如下所示:

          <fragment
              android:id="@+id/some_fragment"
              android:name="package.SomeFragment"
              android:label="Hello {placeholder}"
              tools:layout="@layout/fragment_some">
              <argument
                  android:name="placeholder"
                  app:argType="string" />
          </fragment>
          

          必须使用FragmentDirections(通过操作方法)提供占位符值。

          然后在标题中替换它并显示为Hello World(当placeholder = "World")。

          【讨论】:

            【解决方案7】:

            如果您在活动中,最简单的方法是致电this.setTitle("...")。 如果你在一个片段中,只需调用getActivity().setTitle("...")

            这种方式可以让你随时更改标题,无需在setContentView(R.layout.activity_test)之前调用;

            【讨论】:

              【解决方案8】:

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

              setTitle("new title");
              

              【讨论】:

                【解决方案9】:

                更改操作栏名称的最简单方法是转到 AndroidManifest.xml 并键入此代码。

                <activity android:name=".MainActivity"
                    android:label="Your Label> </activity>
                

                【讨论】:

                  【解决方案10】:

                  更改操作栏名称的最佳方法是转到 AndroidManifest.xml 并输入此代码。

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

                  【讨论】:

                    【解决方案11】:
                    ActionBar ab = getActionBar();
                    TextView tv = new TextView(getApplicationContext());
                    
                    LayoutParams lp = new RelativeLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT, // Width of TextView
                            LayoutParams.WRAP_CONTENT);
                    tv.setLayoutParams(lp);
                    tv.setTextColor(Color.RED);
                    ab.setCustomView(tv);
                    

                    欲了解更多信息,请查看此链接:

                    http://android--code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html

                    【讨论】:

                    • 如果您正在使用 AppCompatActivity,您可以使用 getSupportActionBar()
                    【解决方案12】:
                    getActionBar().setTitle("edit your text"); 
                    

                    【讨论】:

                      【解决方案13】:

                      在 Activity.onCreate() 回调中或在其他需要更改标题的地方:

                      getSupportActionBar().setTitle("Whatever title");
                      

                      【讨论】:

                        【解决方案14】:

                        有点老,但有同样的问题。我是这样做的:

                        strings.xml

                        &lt;string name="title_awesome_app"&gt;My Awesome App&lt;/string&gt;

                        并确保在 AndroidManifest.xml 中进行设置:

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

                        这很简单,您不必担心空引用和其他问题。

                        【讨论】:

                          【解决方案15】:

                          我们可以通过以下两种方式之一更改 ActionBar 标题:

                          1. 在Manifest中:在manifest文件中,设置每个Activity的标签。

                            android:label="@string/TitleWhichYouWantToDisplay"
                            
                          2. 代码中:在代码中调用setTitle()方法,参数为String或String的id。

                            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);
                            
                                }
                            }
                            

                          【讨论】:

                            【解决方案16】:

                            更新:最新的 ActionBar(标题)模式:

                            仅供参考,ActionBar 是在 API 级别 11 中引入的。ActionBar 是 Activity 顶部的一个窗口功能,可以显示 Activity 标题、导航模式和其他交互项目(如搜索)。

                            我完全记得自定义标题栏并使其在应用程序中保持一致。所以我可以和早期做一个比较,可以列出使用ActionBar的一些优点:

                            1. 它为您的用户提供了一个熟悉的跨应用程序界面,系统可以优雅地适应不同的屏幕配置。
                            2. 开发人员不需要编写太多代码来显示 Activity 标题、图标和导航模式,因为 ActionBar 已经准备好进行顶级抽象。

                            例如:

                            => 正常方式,

                            getActionBar().setTitle("Hello world App");   
                            getSupportActionBar().setTitle("Hello world App");  // provide compatibility to all the versions
                            

                            => 自定义操作栏,

                            例如:

                            @Override
                            public void setActionBar(String heading) {
                                // TODO Auto-generated method stub
                            
                                com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
                                actionBar.setHomeButtonEnabled(true);
                                actionBar.setDisplayHomeAsUpEnabled(false);
                                actionBar.setDisplayShowHomeEnabled(false);
                                actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
                                actionBar.setTitle(heading);
                                actionBar.show();
                            
                            }
                            

                            设置操作栏样式:

                            ActionBar 为您提供基本和熟悉的外观、导航模式和其他要执行的快速操作。但这并不意味着它在每个应用程序中看起来都一样。您可以根据您的 UI 和设计要求对其进行自定义。您只需要定义和编写样式和主题。

                            阅读更多:Styling the Action Bar

                            如果您想为 ActionBar 生成样式,那么这个 Style Generator 工具可以帮助您。

                            ================================================ ====================================

                            旧:早期:

                            => 正常方式,

                            您可以通过设置Android:label

                            来更改每个屏幕的标题(即活动)
                               <activity android:name=".Hello_World"
                                              android:label="This is the Hello World Application">
                               </activity>
                            

                            => 自定义 - 标题 - 栏


                            但如果您想以自己的方式自定义标题栏,即 Want to put Image icon and custom-text,那么以下代码适用于我:

                            main.xml

                            <?xml version="1.0" encoding="utf-8"?>
                            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                android:orientation="vertical"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"/>
                            

                            标题栏.xml

                            <?xml version="1.0" encoding="utf-8"?>
                            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                             android:layout_width="400dp" 
                              android:layout_height="fill_parent"
                              android:orientation="horizontal">
                            
                            <ImageView android:id="@+id/ImageView01" 
                                        android:layout_width="57dp" 
                                        android:layout_height="wrap_content"
                                        android:background="@drawable/icon1"/>
                            
                            <TextView 
                            
                              android:id="@+id/myTitle" 
                              android:text="This is my new title" 
                              android:layout_width="fill_parent" 
                              android:layout_height="fill_parent" 
                              android:textColor="@color/titletextcolor"
                               />
                            </LinearLayout>
                            

                            TitleBar.java

                            public class TitleBar extends Activity {
                            
                                @Override
                                public void onCreate(Bundle savedInstanceState) {
                                    super.onCreate(savedInstanceState);
                                    final boolean customTitleSupported = 
                                            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
                                    setContentView(R.layout.main);
                                    if (customTitleSupported) {
                                        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                                            R.layout.titlebar);
                                    }
                                    final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
                                    if (myTitleText != null) {
                                        myTitleText.setText("NEW TITLE");
                                        // user can also set color using "Color" and then
                                        // "Color value constant"
                                        // myTitleText.setBackgroundColor(Color.GREEN);
                                    }
                                }
                            }
                            

                            strings.xml

                            strings.xml 文件定义在 values 文件夹下。

                            <?xml version="1.0" encoding="utf-8"?>
                            <resources>
                                <string name="hello">Hello World, Set_Text_TitleBar!</string>
                                <string name="app_name">Set_Text_TitleBar</string>
                                <color name="titlebackgroundcolor">#3232CD</color>
                                <color name="titletextcolor">#FFFF00</color>
                            </resources>
                            

                            【讨论】:

                            • 按照此示例在 android 中为活动设置自定义标题或为整个应用程序全局设置:labs.makemachine.net/2010/03/custom-android-window-title
                            • +1 !谢谢你,但你能告诉为什么 android:layout_width="400px" ,到父线性布局?
                            • @quinestor 是的,如果需要,您可以使用“wrap_content”或“fill_parent”,这只是示例。
                            • javascript 的一些提示?
                            • 我会反对使用“px”而不是“dp”等独立单位,因为许多新学习者学到的不仅仅是通过尝试分解代码得到答案大部分都没有。
                            【解决方案17】:

                            您可以在Activity 中使用setTitle 以编程方式定义您的标题,此方法可以接受Stringvalues/strings.xml 文件中定义的ID。示例:

                            public class YourActivity extends Activity {
                            
                                @Override
                                public void onCreate(Bundle savedInstanceState) {
                            
                                    setTitle(R.string.your_title);
                                    setContentView(R.layout.main);
                            
                                }
                            }
                            

                            【讨论】:

                              【解决方案18】:

                              尝试这样做...

                              public void onCreate(Bundle savedInstanceState) 
                              {
                              super.onCreate(savedInstanceState);
                                  this.setTitle(String.format(your_format_string, your_personal_text_to_display));
                                  setContentView(R.layout.your_layout);
                                     ...
                                     ...
                              }
                              

                              对我有用

                              【讨论】:

                                【解决方案19】:

                                您可以在清单文件中为每个活动定义标签。

                                活动的正常定义如下所示:

                                <activity
                                     android:name=".ui.myactivity"
                                     android:label="@string/Title Text" />
                                

                                标题文本应替换为此活动的字符串资源的 id。

                                如果你想动态设置,你也可以从代码中设置标题文本。

                                setTitle(address.getCity());
                                

                                在我的活动的 oncreate 方法中,标题被设置为特定地址的城市。

                                【讨论】:

                                  猜你喜欢
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2016-10-22
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2012-07-31
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多