Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在主要活动中设置标题_Android_Android Titlebar - Fatal编程技术网

Android 在主要活动中设置标题

Android 在主要活动中设置标题,android,android-titlebar,Android,Android Titlebar,我很难为情地发布这个问题,但我花了两个小时试图更改我的应用程序的主要活动中的标题,但出乎意料的是,没有结果。我试过: 片名; getActionBar.setTitle getSupportActionBar.setTitle 每个人都在写上述内容,但没有一个有效。更令人惊讶的是,在其他活动和片段中,一切都很好。只有这一个 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved

我很难为情地发布这个问题,但我花了两个小时试图更改我的应用程序的主要活动中的标题,但出乎意料的是,没有结果。我试过: 片名; getActionBar.setTitle getSupportActionBar.setTitle

每个人都在写上述内容,但没有一个有效。更令人惊讶的是,在其他活动和片段中,一切都很好。只有这一个

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

    Utils.checkGpsStatus(this);
    Utils.checkNetworkStatus(this);

    Location location = SmartLocation.with(this).location().getLastLocation();
    if(location != null){
        lat = location.getLatitude();
        lng = location.getLongitude();
    }

    ViewPager viewPager = (ViewPager) findViewById(R.id.activity_main_activity_beer);
    if (viewPager != null) {
        setupViewPager(viewPager);
    }
    //viewPager.setOffscreenPageLimit(2);
    //viewPager.setCurrentItem(1);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}
XML:

res/values/strings.xml

<resources>
    <string name="app_name">insert here title</string>
</resources>
试试这个

使用extends AppCompatActivity扩展您的活动

在onCreate方法中放入以下代码

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setElevation(0);
    getSupportActionBar().setTitle("Your Title");
干杯

private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}


这是使您的代码与最新设备兼容的最佳做法,因为默认actionbar已从Lollipop中弃用。

您可以发布您的活动代码和布局xml文件吗?请发布出现此错误的类。@MohammedAtif已用代码更新,请不要否决-这看起来像是IDE错误,因为没有办法设定标题;无法工作。您也可以添加您的XML文件吗?我想以编程方式更改它。这是Android Studio在没有我们干预的情况下执行的默认代码。在这里,他想通过编程设置标题;应该可以,但你说不行,令人惊讶的是你的onCreate中有这个吗?这样,您必须在每个活动中插入它来设置标题
private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />