工具栏在布局中不可见。

4

以下是我的布局,在运行时工具栏根本不可见,选项卡视图中的两个选项卡占据了整个屏幕。在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"));
2个回答

6
请使用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>

3

您的主要布局容器是RelativeLayout,因此您需要明确地将子视图相对定位。为了使您的内容显示在工具栏下方(而不是覆盖它),请使用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代码(Android Studio-> Code-> Reformat Code),那将会很有帮助。除非格式化,否则很难理解正在发生什么。

谢谢。那个有效了,但现在我看不到每个选项卡顶部应该有的选项卡标题指示器,直接位于工具栏下方。 - ez4nick
尝试将您的TabLayout设置为android:layout_height="wrap_content" - hungryghost

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接