android - 工具栏在布局中不可见

标签 android android-toolbar

下面是我的布局,运行时toolbar根本不可见,tabview上的两个tab占据了整个屏幕。在 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"));

最佳答案

改为使用 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>

关于android - 工具栏在布局中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949209/

相关文章:

java - org.json.JSONException : Value test( of type java. lang.String 无法转换为 JSONObject

java - 如何为 ListView 创建自定义光标适配器以用于图像和文本?

java - 尝试在 null 对象上调用虚拟方法 'void androidx.appcompat.widget.Toolbar.setNavigationIcon(android.graphics.drawable.Drawable)'

android - 从溢出菜单进入/退出动画中删除额外的背景颜色矩形?

java - 即使 ItemAnimator.canReuseUpdatedViewHolder() 返回 true,OnCreateViewHolder 仍然被调用

android - 如何更改无限进度条的颜色?

android - ActionBar compact with 3 tabs : issue with fragments

带有扩展工具栏的 Android Material

android - 如何使SearchView覆盖整个ActionBar/Toolbar

android - 将滑动标签与工具栏一起使用