【问题标题】:Android Toolbar TitleAndroid 工具栏标题
【发布时间】:2017-03-21 05:34:04
【问题描述】:

无法更改工具栏标题我在manifeast.xml 中设置了标题,并且还使用了setTitle("TITLE");

我将标题设置为 History & Reports,但它显示不同的标题 Notifications,这是另一个活动标题。

我检查了manifeast.xml 但没有变化,谁能帮我解决这个问题。

这是我的代码

public class Nav_HistoryAndReports extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("History & Reports");
    setContentView(R.layout.nav_history_and_reports);

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

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

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

public void setToolbar(Toolbar toolbar) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FragmentHistoryAndReports_Visits(), "VISITS");
    adapter.addFragment(new FragmentHistoryAndReports_Visitors(), "VISITORS");
    adapter.addFragment(new FragmentHistoryAndReports_Map(), "MAP");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:titleTextColor="@color/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:theme="@style/GalaxyZooThemeToolbarDarkOverflow">

</android.support.v7.widget.Toolbar>

【问题讨论】:

  • 请在您设置标题“历史”的地方添加代码。
  • 对不起,标题是历史和报告
  • 无论您设置什么标题,都可以在您的问题上添加该代码。
  • setToolbar() 之后的 setTitle()
  • 在您的 setToolbar() 方法结束时或之后调用 getSupportActionBar().setTitle("History &amp; Reports");

标签: android android-manifest android-appbarlayout android-titlebar


【解决方案1】:

工具栏标题有两种设置方式

1.在布局XML中

<android.support.design.widget.AppBarLayout

android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="4dp"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="Main Activity"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:navigationIcon="@drawable/ic_arrow_back_white_24dp"

        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

2。在 Java 中 它将在 AppCompatActivity 中工作

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Notice Board"); 
setSupportActionBar(toolbar);

【讨论】:

  • 确保活动是 AppCompatActivity
【解决方案2】:

我使用下面的行来更改工具栏标题

getSupportActionBar().setTitle("YOUR_TITLE");

【讨论】:

  • @Listo,很高兴它有帮助。
【解决方案3】:

toolbar = findViewById(R.id.toolbar);这一行之后添加toolbar.setTitle("History &amp; Reports");

并删除setTitle("History &amp; Reports");

【讨论】:

  • 感谢您的快速回复,但没有任何变化
  • @Listo 也通过 Rachit 建议添加此内容 getSupportActionBar().setDisplayShowTitleEnabled(true);
【解决方案4】:

在你的布局中设置标题,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.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_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        android:titleTextColor="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:navigationIcon="?attr/homeAsUpIndicator"
        app:theme="@style/GalaxyZooThemeToolbarDarkOverflow">

        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_marginLeft="5dp"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"/>

    </android.support.v7.widget.Toolbar>

在您的活动中只需设置工具栏并禁用其默认标题,如下所示

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

    ActionBar actionBar = getSupportActionBar();
    if(actionBar != null)
        actionBar.setDisplayShowTitleEnabled(false);

【讨论】:

    【解决方案5】:

    稍微改变你的代码看起来像这样..

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

    setToolbar 之后看看

     public void setToolbar(Toolbar toolbar) {
        toolbar.setTitle("History & Reports");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    【讨论】:

      【解决方案6】:

      正如Mike M所说

      我刚刚在您的 setToolbar() 方法的末尾或调用之后添加了getSupportActionBar().setTitle("History &amp; Reports");

      【讨论】:

        【解决方案7】:

        尝试添加 getsupportActionBar().setTitle();

        【讨论】:

        • setSupportActionBar(toolbar) - 他们已经在使用NoActionBar 主题了。
        【解决方案8】:

        我找到了解决办法

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null)
                getSupportActionBar().setTitle(getString(R.string.calender));
        

        这个对我有用

        【讨论】:

          【解决方案9】:

          如果您使用支持actionBar的工具栏,您应该按照以下代码进行:

          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_favorite);
          
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          
          try {
              getSupportActionBar().setDisplayHomeAsUpEnabled(true);
              getSupportActionBar().setDisplayShowHomeEnabled(true);
          } catch (NullPointerException e) {
              Log.d(TAG, "onCreate: back button not supported!");
          }
          
          // here you set your activity title
          getSupportActionBar().setTitle("Your Activity Title");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-12-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-03
            • 1970-01-01
            相关资源
            最近更新 更多