无法更改工具栏标题我在manifeast.xml
&中设置了标题还使用了setTitle("TITLE");
我将标题设为历史&报告,但它显示不同的标题通知,这是另一个活动标题。
我查了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>
答案 0 :(得分:4)
工具栏标题可以通过两种方式设置
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);
答案 1 :(得分:2)
我使用下面的行来更改工具栏标题
getSupportActionBar().setTitle("YOUR_TITLE");
答案 2 :(得分:1)
在此行toolbar = findViewById(R.id.toolbar);
之后添加toolbar.setTitle("History & Reports");
并删除setTitle("History & Reports");
答案 3 :(得分:1)
在布局中设置标题,如下所示
<?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);
答案 4 :(得分:1)
我找到了解决方案
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setTitle(getString(R.string.calender));
这个对我有用
答案 5 :(得分:1)
如果您使用支持@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");
的工具栏,则应执行以下代码:
.mat-menu-item{
max-height: 50px;
}
答案 6 :(得分:0)
尝试添加getsupportActionBar()。setTitle();
答案 7 :(得分:0)
Litle改变你的代码看起来像这样..
toolbar = (Toolbar) findViewById(R.id.toolbar);
setToolbar(toolbar);
在setToolbar之后看
public void setToolbar(Toolbar toolbar) {
toolbar.setTitle("History & Reports");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
答案 8 :(得分:0)
正如 Mike M 所说
我刚刚在调用你的setToolbar()方法结束时或之后添加了getSupportActionBar().setTitle("History & Reports");
。