工具栏在布局中不可见

Toolbar not visible in layout

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

这是我的 activity 我正在使用此布局的位置:

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"));

您的主要布局容器是 RelativeLayout,因此您需要明确定位您的子视图 relative 彼此。要使您的内容显示在工具栏下方(而不是上方),请将其放置在 android:layout_below="@+id/my_toolbar2" 位置。它应该看起来像这样:

<RelativeLayout
    ...
    android:id="@+id/c"
    ... >

    <android.support.v7.widget.Toolbar
        ...
        android:id="@+id/my_toolbar2"
        ... />

    <RelativeLayout
        ...
        android:id="@+id/c2"
        android:layout_below="@+id/my_toolbar2" 
        ... >

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

        <android.support.v4.view.ViewPager
            ...
            android:id="@+id/pager"
            ... />

    </RelativeLayout>

</RelativeLayout>

另外,请注意,我已将您的 ViewPager 嵌套在内部 RelativeLayout(主要内容容器)中,以便它与您的 TabLayout 放在一起。

另外一件事:如果您可以在发布代码之前正确格式化 XML 或 Java(AndroidStudio -> 代码 -> 重新格式化代码),将会很有帮助。除非格式化,否则很难阅读正在发生的事情。

改为使用 LinearLayout 作为根布局。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
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" />

 <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
    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.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>

    </RelativeLayout>






</LinearLayout>