下面是我的佈局,當運行工具欄時根本不可見,tabview上的兩個選項卡佔據了整個屏幕。在AndroidStudio的預覽中,我確實看到了ActionBar,因爲我期望它在頂部。我錯過了什麼?工具欄在佈局中不可見
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/c">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="?attr/colorPrimary"
android:layout_marginBottom="10dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/c2">
<android.support.design.widget.TabLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/tabs"
app:layout_scrollFlags="scroll|enterAlways"/>
</RelativeLayout>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
這裏是我的活動,我現在用這個佈局:
public class TabletGallery extends AppCompatActivity implements ActionBar.TabListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates.
setContentView(R.layout.tablet_gallery);
// Initilization
toolbar = (Toolbar) findViewById(R.id.my_toolbar2);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
謝謝。這工作,但現在我看不到應該在每個標籤的頂部和工具欄正下方的標籤標題指示器 – ez4nick
嘗試將您的TabLayout設置爲'android:layout_height =「wrap_content」'。 – hungryghost
@hungryghost,謝謝,它爲我工作:) – Ramesh