简体   繁体   English

如何在Fragment Android中的操作栏中更改标题?

[英]How to Change Title in Action Bar in Fragment Android?

This my MainActivity 这是我的MainActivity

public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

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

    /**
     *Setup the DrawerLayout and NavigationView
     */

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

    /**
     * Lets inflate the very first fragment
     * Here , we are inflating the TabFragment as the first Fragment
     */

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new Recommendation()).commit();
    /**
     * Setup click events on the Navigation View Items.
     */

    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            mDrawerLayout.closeDrawers();



            if (menuItem.getItemId() == R.id.nav_item_lux_level_recomen) {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,new Recommendation()).commit();

            }

            if (menuItem.getItemId() == R.id.nav_item_room_index_calc) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new RoomIndex()).commit();
            }

            if (menuItem.getItemId() == R.id.nav_item_utilization_factor_calc) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new UtilizationFactorCalculator()).commit();
            }

            if (menuItem.getItemId() == R.id.nav_item_conversions) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new Conversion()).commit();
            }


            return false;
        }

    });

    /**
     * Setup Drawer Toggle of the Toolbar
     */

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
            R.string.app_name);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerToggle.syncState();

}

} }

This my activity_main.xml 这是我的activity_main.xml

<LinearLayout
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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:id="@+id/toolbar"
    />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    >



    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">
    </FrameLayout>



    <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/colorPrimary"
        app:menu="@menu/drawermenu"
        android:layout_marginTop="0dp"
        />



</android.support.v4.widget.DrawerLayout>

This my Activity that i want change the title when it clicked 单击此我想更改标题的活动

public class RoomIndex extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View x =  inflater.inflate(R.layout.room_index,null);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Room Index");
    return x;
}

} }

I have tried ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Room Index"); 我已经尝试过((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(“ Room Index”); But it returns error Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference 但是它返回错误尝试在空对象引用上调用虚拟方法'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)'

I dont know whats wrong, i searched over the internet and try it, and it return the same. 我不知道怎么了,我在互联网上搜索并尝试了一下,结果还是一样。

First set Toolbar as your SupportActionBar in onCreate() of Activity: 首先在Activity的onCreate()中将工具栏设置为您的SupportActionBar:

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

Then Use setTitle to Change Toolbar title: 然后使用setTitle更改工具栏标题:

getActivity().setTitle("Your title");

Hope this helps. 希望这可以帮助。

You need to set the Support action bar first in the onCreate method before you can retrieve it. 您需要先在onCreate方法中设置“支持”操作栏,然后才能检索它。

Modify you code to this: 将您的代码修改为此:

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

UPDATE 更新

Checking from the source code, you may need to add this code after setting the Support ActionBar: 从源代码检查,设置支持操作栏后,可能需要添加以下代码:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM