【发布时间】:2021-06-23 23:13:38
【问题描述】:
<?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 代码中
hare is a screenshot
它只显示选项卡和选项卡名称,但不显示选项卡。
它显示标题但不显示片段。
它在标签中显示标题,它们是聊天、用户,但不显示片段。我还有一个工具栏和一个操作栏。请帮帮我。
【问题讨论】:
-
向我们展示您的实现。如果您不就当前的方法提供见解,任何人都无法为您提供帮助。
-
请帮忙,我已经上传了代码
-
您的代码很好,似乎是布局问题。在这里分享整个布局。
-
现在我已经上传了完整的 xml 文件
标签: android android-fragments android-tablayout