【问题标题】:How do I change the text in the toolbar in each activity?如何更改每个活动工具栏中的文本?
【发布时间】:2019-11-19 17:34:37
【问题描述】:

对此有一些问题的回答,但是,它们似乎不适用于新的 AndroidX

当我想让它说别的东西时,我的主要活动在工具栏中说“主页”。我只使用 Android Studio 中的默认导航抽屉活动

这是我的主要 Activity.java

public class MainActivity extends AppCompatActivity {






@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Toolbar toolbar = findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

假设我想将工具栏的文本设置为某个东西,我该怎么做?

这里还有styles.xml供参考

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

【问题讨论】:

    标签: android android-studio android-toolbar settext


    【解决方案1】:

    根据您的要求,您可以使用以下方法静态更改它:

    getSupportActionBar().setTitle("Some Title Here");

    【讨论】:

      【解决方案2】:

      有几种方法可以做到这一点,因为我使用 Material Design Dependency 我将我的 AppThemeresources 更改为此

      <resources>
      
          <!-- Base application theme. -->
         <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar>
      
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
          </style>
      
      </resources>
      
      

      具体来说,这是我正在谈论的标签

      <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar>
      

      编辑:如果你想要 Material Design Dependency,这里是:

      // New Material Design
          implementation 'com.google.android.material:material:1.0.0'
      


      然后转到您特定活动的 XML 并添加自定义工具栏,如下所示:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity"
          android:orientation="vertical">
      
          <androidx.appcompat.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/colorPrimary"/>
      
      
      
      </LinearLayout>
      

      注意,这里我使用 线性布局 只是为了不与约束混淆..

      然后进入你的活动,这样做:

      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              setSupportActionBar(toolbar)// this is your toolbar ID
              setTitle("Hello World") // your toolbar title
      
          }
      

      谢谢!

      【讨论】:

        【解决方案3】:
        <?xml version="1.0" encoding="utf-8"?>
        <androidx.appcompat.widget.Toolbar>
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"android.support.v7.widget.Toolbar
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="?attr/colorPrimary"
            android:text="<your text>">
        
        </androidx.appcompat.widget.Toolbar>
        

        【讨论】:

        • 但是我在哪里输入我想要的文本?
        • @JohnB check 我已经编辑了你可以编辑文本的地方
        猜你喜欢
        • 2018-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多