下面是我的布局,当运行工具栏时根本不可见,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