【问题标题】:how to set toolbar on FragmentActivity?如何在 FragmentActivity 上设置工具栏?
【发布时间】:2016-04-24 12:57:37
【问题描述】:

我想在扩展 FragmentActivity 的 Activity 上设置 工具栏。我知道为了使用setSuppoertActionBar(toolbar) 方法,我们扩展了AppCompatActivity 而不是FragmentActivity,但是我覆盖了onMenuItemSelected(int featureId, MenuItem item) 方法,它在AppCompatActivity 中是final 的,而final 方法不能覆盖。所以我仅限于扩展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) 方法。

我该怎么办,请帮忙。

【问题讨论】:

  • 试过onOptionsItemSelected了吗?
  • 是的,我明白了,我根据您的建议找到了解决方案。我扩展了 AppCompatActivity 而不是 FragmentActivity 并使用 onOptionsItemSelected() 方法更改了 onMenuItemSelected() 方法。谢谢:)

标签: android android-fragmentactivity android-toolbar


【解决方案1】:

这个东西在你使用NavigationDrawer时很好 使用这个:-

 toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

然后根据onNavigationItemSelected中不同Title的不同片段设置Toolbar Title:-

@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:-

<style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
// ToDo as you want to do or as per your requirement

</style>

然后应用到您的自定义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>

【讨论】:

    【解决方案2】:

    您也可以创建自己的toolbar

    先设置主主题扩展Theme.AppCompat.Light.NoActionBar

    <style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    
    </style>
    

    记得应用主题:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setTheme(R.style.style_1);
        // ...
    }
    

    然后在您的Activity 的xml 中您可以设置自己的自定义toolbar

    <include layout="@layout/my_toolbar"/>
    

    @layout/my_toolbar 可能如下所示:

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        app:contentInsetStart="0dp"
        app:layout_collapseParallaxMultiplier="1.0">
    
        <!-- insert your views here -->
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      【解决方案3】:
      First set style as  
      <style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
          <!-- Customize your theme here. -->
      
      </style>
      then
      
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
      ActionBar actionBar = getSupportActionBar();
              actionBar.setTitle("Home");
      

      【讨论】:

        【解决方案4】:

        您应该从 AppCompatActivity 扩展您的 Activity,因为这包括对 Fragments 和工具栏的支持。 然后覆盖

        onCreateOptionsMenu() 和 onOptionsItemSelected() 方法

        你不应该覆盖

        onMenuItemSelected()

        public class MainActivity extends AppCompatActivity { ...
        
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
            ....
        
        
            ....
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
              ...
                 ...
            }
        
        
           @Override
           public boolean onOptionsItemSelected(MenuItem item) {
        
           ...
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多