我的活动中有Viewpager,其中有三个片段。但是那些片段的标题是隐藏的。
我用谷歌搜索,但是没有找到合适的解决方案。
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/main_toolbar"
layout="@layout/app_bar_layout">
</include>
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/main_view_pager"
android:layout_width="match_parent"
android:layout_height="464dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="103dp"/>
</RelativeLayout>
这是我的tabspageradapter类,其中为这些片段提供标题:
package com.example.user.messingaround;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
RequestsFragment requestsFragment = new RequestsFragment();
return requestsFragment;
case 1:
ChatsFragment chatsFragment = new ChatsFragment();
return chatsFragment;
case 2:
FriendsFragment friendsFragment = new FriendsFragment();
return friendsFragment;
default:
return null;
}
}
@Override
public int getCount()
{
return 3;
}
public CharSequence getTitle(int position)
{
switch (position)
{
case 0:
return "Requests";
case 1:
return "Chats";
case 2:
return "Friends";
default:
return null;
}
}
}
这是我的活动类,我在其中初始化我的viewpager:
viewPager = (ViewPager)findViewById(R.id.main_view_pager);
tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsPagerAdapter);
tabLayout = (TabLayout)findViewById(R.id.main_tabs);
tabLayout.setupWithViewPager(viewPager);
mtoolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(mtoolbar);
getSupportActionBar().setTitle("MyChat");
}
答案 0 :(得分:0)
我认为您的xml是不正确的,请尝试我的代码,其中的代码行很少。
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/TabStyle"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
如果仍然遇到问题,请尝试以下链接:https://android.jlelse.eu/tablayout-and-viewpager-in-your-android-app-738b8840c38a