Android-不显示TabLayout标题

时间:2018-08-01 14:23:10

标签: android android-layout android-fragments android-tablayout

我遇到了问题,想知道有人可以帮助我!我一直在尝试在这里实现这种布局:

enter image description here

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

代码
<?php 
session_start();
...

ViewPagerAdapter.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);

}

activity_main.xml

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);
}
}

FragmentFlood.java

<?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>

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

4 个答案:

答案 0 :(得分:1)

viewPager.setAdapter(adapter);    
tabLayout.setupWithViewPager(viewPager);//<-This line should be after setAdapter to view pager

答案 1 :(得分:0)

将此添加到您的活动中

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

答案 2 :(得分:0)

我认为您应该选中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"
        />

参见下图 enter image description here

答案 3 :(得分:0)

tabLayout.getTabAt(0).setText("Hello");
tabLayout.getTabAt(1).setIcon(R.drawable.ic_action_call);

请尝试这个。