工具栏在布局中不可见

时间:2015-06-20 00:53:53

标签: android android-toolbar

下面是我的布局,当运行工具栏完全不可见时,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"));

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:3)

您的主布局容器是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 - &gt;代码 - &gt;重新格式化代码)。除非格式化,否则读取正在发生的事情有点困难。