设置ActionBar上所选项目的标题

时间:2015-12-22 22:16:52

标签: android android-actionbar android-navigation-drawer

你好我一直试图让ActionBar的标题显示我在navigationDrawer中选择的Title。我附上代码,请帮助我。 我现在的意思是:   - 我有一个导航抽屉有一些标题导入,画廊。 问题是:   - 当我选择导航抽屉中的一个项目时,它不会在操作栏上显示标题名称,这会使我的应用程序看起来不完整。 我需要的是:   - 要在我选择的ActionBar / ToolBar上显示项目名称。 这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

NavigationView navigationView;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //set the fragment initially
    MainFragment fragment = new MainFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.Fragment_Layout,fragment);
    fragmentTransaction.commit();

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.Fragment_Layout, fragment);
        fragmentTransaction.commit();
    } else if (id == R.id.nav_gallery) {
        SecondFragment fragment = new SecondFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.Fragment_Layout,fragment);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

图像在导航抽屉中选择ttle之前和之后。 之前: enter image description here

选择图库后,您可以看到页面上的第二个按钮出现在ActionBar中的按钮,它仍然显示欢迎。请帮助我。 后: enter image description here

先谢谢你。

2 个答案:

答案 0 :(得分:1)

在工具栏中添加文本视图

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/toolbar_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@android:style/Theme.Light">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Toolbar Title"
            android:textColor="@color/textColorWhite"
            android:textSize="16sp" />

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

禁用工具栏标题。

getSupportActionBar().setDisplayShowTitleEnabled(false);

设置自定义textview的标题

((TextView) toolbar.findViewById(R.id.toolbar_title)).setText(title);

答案 1 :(得分:1)

尝试:

 ActionBar actionBar = getActivity().getSupportActionBar();
 actionBar.setDisplayShowTitleEnabled(true);
 actionBar.setTitle("Narnia");

您可以创建一个方法,然后在onNavigationItemSelected中,并使用您想要的标题调用该方法。