【发布时间】:2018-08-01 14:23:10
【问题描述】:
我遇到了一个问题,想知道任何人都可以帮助我!我一直在尝试在这里实现这种布局:
我的问题是 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,但我在此处发布的代码中省略了它。请有人帮帮我。
【问题讨论】:
-
你可以编码片段吗?我检查了上面的代码,它仍然显示标题
-
@MeosCoder 它不起作用:(它只显示一个空白的白色标签布局。请帮助我:(
-
所有 3 个标签都没有显示标题?你可以给我看屏幕截图吗?
-
@MeosCoder 这里是image.ibb.co/mTYOiz/Screen_Shot_2018_08_03_at_12_25_36_PM.png 仍然无法正常工作:(
标签: android android-layout android-fragments android-tablayout