Android - TabLayout 标题不显示

Android - TabLayout titles not displaying

我遇到了一个问题,想知道任何人都可以帮助我!我一直在努力实现这里的布局:

我的问题是 TabLayout 标题现在没有显示。 这是我的 MainActivity.java

代码
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener, OnMapReadyCallback{

private MapboxMap mapboxMap;
private MapView mapView;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tabLayout = (TabLayout)findViewById(R.id.tablayout_id);
    viewPager = (ViewPager)findViewById(R.id.viewpager_id);

    tabLayout.setupWithViewPager(viewPager);

    //Creates the fragment (tab)
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FragmentFlood(),"Flood");
    adapter.addFragment(new FragmentWeather(), "Weather");
    adapter.addFragment(new FragmentAnalytics(),"Analytics");

    //pass view to view pager
    viewPager.setAdapter(adapter);

}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter{

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();

public void addFragment(Fragment fragments, String titles){
    this.fragmentList.add(fragments);
    this.FragmentListTitles.add(titles);
    Log.d("fragments", "fragg");

}

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public int getCount() {
    return fragmentList.size();
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return FragmentListTitles.get(position);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
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_height="match_parent"
android:layout_width="match_parent">

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="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"
    tools:layout_editor_absoluteY="81dp">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_id"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:layout_marginTop="300dp"
        android:background="@color/colorPrimary"
        android:elevation="2dp"
        app:layout_constraintBottom_toTopOf="@+id/viewpager_id"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimary"
        app:tabTextColor="@color/tabTextColor" />

    <android.support.v4.view.ViewPager
        android:layout_width="377dp"
        android:layout_height="163dp"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="350dp"
        tools:ignore="MissingConstraints"
        android:id="@+id/viewpager_id"/>


</android.support.constraint.ConstraintLayout>

</ScrollView>

FragmentFlood.java

public class FragmentFlood extends Fragment{
View view;


public FragmentFlood() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view=inflater.inflate(R.layout.flood_fragment,container,false);
    return view;
}
}

没有 logcat 错误,一切似乎都很好。但是当我 运行 它时,TabLayout 根本不显示任何东西?我在视图中也有 MapBox,但我在此处发布的代码中省略了它。请有人帮帮我。

将此添加到您的 activity

tabLayout.addTab(tabLayout.newTab().setText("Flood"));
tabLayout.addTab(tabLayout.newTab().setText("Weather"));
tabLayout.addTab(tabLayout.newTab().setText("Analytics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

我认为你应该检查 app:tabTextColor 并更改为不同的颜色,例如:黑色、白色... app:tabTextColor="@android:color/black"

<android.support.design.widget.TabLayout
        android:id="@+id/tablayout_id"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:layout_marginTop="300dp"
        android:background="@color/colorPrimary"
        android:elevation="2dp"
        app:layout_constraintBottom_toTopOf="@+id/viewpager_id"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimary"
        app:tabTextColor="@android:color/black"
        />

见下图

viewPager.setAdapter(adapter);    
tabLayout.setupWithViewPager(viewPager);//<-This line should be after setAdapter to view pager
tabLayout.getTabAt(0).setText("Hello");
tabLayout.getTabAt(1).setIcon(R.drawable.ic_action_call);

请试试这个。