问题描述
我想在扩展 FragmentActivity 的活动上设置工具栏.我知道,对于使用setSuppoertActionBar(toolbar)
方法,我们扩展了AppCompatActivity
而不是FragmentActivity
,但是我覆盖了onMenuItemSelected(int featureId, MenuItem item)
方法,该方法在AppCompatActivity
中是最终的,并且final方法无法覆盖.所以我只能扩展FragmentActivity
.
I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar)
method we extends AppCompatActivity
instead of FragmentActivity
but I override the onMenuItemSelected(int featureId, MenuItem item)
method which is final in AppCompatActivity
and final method cannot override. so I'm restricted to extends FragmentActivity
.
这是我的代码:
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar); -> error is here
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
onSearchRequested();
break;
}
return super.onMenuItemSelected(featureId, item);
}
我看到了很多与此问题相关的答案,但每个人都说扩展了AppCompatActivity
而不是FragmentActivity
,但是我想设置工具栏以及覆盖onMenuItemSelected(int featureId, MenuItem item)
方法.
I saw many answers related to that question but everyone says extends AppCompatActivity
instead of FragmentActivity
but I want to set toolbar as well as override onMenuItemSelected(int featureId, MenuItem item)
method.
我该怎么办,请帮忙.
推荐答案
当您使用NavigationDrawer
时,这东西很好使用此:-
This thing is good when you are using NavigationDrawer
use this:-
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
然后根据onNavigationItemSelected
中的具有不同标题的不同片段设置工具栏标题:-
then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected
:-
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
sfm = getSupportFragmentManager();
Fragment fragment = new Fragment();
int id = item.getItemId();
if (id == R.id.nav_id) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}else if (id == R.id.nav_id2) {
fragment = new YourFragment();
toolbar.setTitle("SET TOOLBAR NAME");
}
对于单个片段,首先像这样自定义style.xml
:-
For single fragment, first customize your style.xml
like this :-
<style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
// ToDo as you want to do or as per your requirement
</style>
然后将其应用于您的自定义toolbar
:-
then apply into your custom toolbar
:-
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/YourStyleName" >
// ...........
</android.support.v7.widget.Toolbar>
这篇关于如何在FragmentActivity上设置工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!