android - 为什么片段没有显示在tablayout中
问题描述
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
android:orientation="horizontal"
android:fillViewport="true
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/MenuStyle">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_height="30dp"
android:layout_width="30dp"
android:id="@+id/profile_image" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/display_username"
android:layout_marginLeft="25dp"
android:textColor="#ffff"
android:textStyle="bold"
android:layout_marginStart="25dp"/>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab_layout"
android:background="#2F84C8"
app:tabSelectedTextColor="#fff"
app:tabIndicatorColor="#f66"
app:tabTextColor="#fff"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
在xml代码中
TabLayout tabLayout=findViewById(R.id.tab_layout);
ViewPager viewPager=findViewById(R.id.viewpager);
ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new ChatsFragment(),"Chats");
viewPagerAdapter.addFragment(new UserFragment(),"Users");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
class ViewPagerAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment>fragments;
private ArrayList<String> titles;
ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments=new ArrayList<>();
this.titles=new ArrayList<>();
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
titles.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
在 Java 代码
野兔是截图
它仅显示选项卡和选项卡名称,但不显示选项卡。
它显示标题但不显示片段。
它在标签中显示标题,它们是聊天、用户,但不显示片段。我还有一个工具栏和一个操作栏。请帮我。
解决方案
Possibility 1: because you use RelativeLayout
without some layout_below
tag, the content could be under AppBar
Possibility 2: ViewPager
doesn't need android:fillViewport="true"
and app:layout_behavior="@string/appbar_scrolling_view_behavior"
Possibility 3: remove android:orientation="horizontal"
from RelativeLayout
. Is not necessary. Try to change to ConstraintLayout
OR: provide code from your fragment initialisation. Perhaps content is not setted.
推荐阅读
- javascript - 将数据从 Javascript 发送回 Python
- c# - 在 Unity 中将 Sprite 数组更改为 GameObject 数组
- javascript - 使用来自 Web 服务的 SAPUI5 和 SOAP 请求和响应
- python - Python RecursionError : Pandas.eval() 的简单操作崩溃
- jenkins - Jenkins 没有从 Workspace 中的 Katalon 测试创建 JUnit 测试报告
- r - 如何使用 R 删除数据框中的第一行和最后 N 行?
- java - 我正在尝试制作一个接受两个数字并在两者之间找到素数、偶数和奇数的程序
- html - 内联时对齐文本
- ibm-cloud - 使用“请求客户端证书”配置 IBM Cloud 应用程序
- batch-file - RENAME 函数不在批处理脚本中重命名