2016-07-05 116 views
-3

我很尷尬地發佈這個問題,但我試圖在兩個小時內更改應用程序的主要活動中的標題,並且令我驚訝的是沒有結果。我想:
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> 

+0

你可以發佈你的活動代碼以及你的佈局xml文件嗎? – Eenvincible

+0

請發佈您遇到此錯誤的課程。 –

+0

@MohammedAtif更新了代碼 –

回答

1

RES /價值/ strings.xml中

<resources> 
    <string name="app_name">insert here title</string> 
</resources> 
+0

我想以編程方式更改它 –

+0

這就是Android Studio在沒有我們干預的情況下執行的默認代碼。在這裏,他想以編程方式設置標題。 –

+0

@MohammedAtif這是正確的,thx –

0

試試這個

與擴展AppCompatActivity拓展業務。

將下面的代碼放在您的onCreate方法中。

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

乾杯!

0
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" /> 

這是使你的代碼與最新的設備兼容,因爲默認動作條是從棒棒堂過時的最佳實踐。

相關問題