工具栏在布局中不可见
问题描述:
下面是我的布局,当运行工具栏时根本不可见,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"));
答
改为使用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>
答
你的主要布局容器是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(AndroidStudio - >代码 - >重新格式化代码),这会很有帮助。除非格式化,否则阅读正在进行的内容有点困难。
谢谢。这工作,但现在我看不到应该在每个标签的顶部和工具栏正下方的标签标题指示器 – ez4nick
尝试将您的TabLayout设置为'android:layout_height =“wrap_content”'。 – hungryghost
@hungryghost,谢谢,它为我工作:) – Ramesh