Настройка заголовка в основной деятельности

Мне стыдно написать этот вопрос, но я пытаюсь в течение двух часов изменить заголовок в основной области действия моего приложения и, к моему удивлению, безрезультатно. Я старался:
setTitle();
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:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainBeerActivity" >

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal"
        android:background="?attr/colorPrimary"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/activity_main_activity_beer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

3 ответа

RES / значения /strings.xml

<resources>
    <string name="app_name">insert here title</string>
</resources>
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" />

Это лучший способ сделать ваш код совместимым с последними устройствами, потому что панель действий по умолчанию устарела из Lollipop.

Попробуй это

Расширьте свою деятельность, расширяя AppCompatActivity.

Поместите код ниже в ваш метод onCreate.

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

Ура!

Другие вопросы по тегам