Java 为什么我的选项卡式活动中的文本没有显示?
我尝试手动创建选项卡式活动。在我的预览XML中,它显示了如下图所示的文本。 但当我在手机上运行这些应用程序时,它并没有显示文本。我想知道为什么? 有人能告诉我我做错了什么吗 我遵循代码并尝试使用自己的代码,因为android提供的新片段很难理解,所以我决定按照教程手动执行,但当我通过手机运行应用程序时,我发现文本没有显示出来。我想我错过了一些东西,我不太确定我错过了哪一步。如果有人能在我的xml布局页面中帮助我了解这个问题,我将非常感激 多谢各位Java 为什么我的选项卡式活动中的文本没有显示?,java,android,xml,android-fragments,android-fragmentactivity,Java,Android,Xml,Android Fragments,Android Fragmentactivity,我尝试手动创建选项卡式活动。在我的预览XML中,它显示了如下图所示的文本。 但当我在手机上运行这些应用程序时,它并没有显示文本。我想知道为什么? 有人能告诉我我做错了什么吗 我遵循代码并尝试使用自己的代码,因为android提供的新片段很难理解,所以我决定按照教程手动执行,但当我通过手机运行应用程序时,我发现文本没有显示出来。我想我错过了一些东西,我不太确定我错过了哪一步。如果有人能在我的xml布局页面中帮助我了解这个问题,我将非常感激 多谢各位 <?xml version="1.0"
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PAID"
android:textColor="#FFFFFF"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UNPAID"
android:textColor="#FFFFFF"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM POSTED"
android:textColor="#FFFFFF"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
您必须通过实现
getPageTitle
来从部分SpagerAdapter
设置标题,如下所示:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
....
@Nullable
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch(position){
case 0:
title = "PAID";
break;
case 1:
title = "UNPAID";
break;
case 2:
title = "ITEM POSTED";
break;
}
return title;
}
....
}
我没有在xml中使用初始化选项卡,但是您可以使用这种方式在活动中实现选项卡
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
app:tabGravity="center"
android:layout_height="wrap_content">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
@lastgg,我的答案有什么问题?你为什么在19小时后回复被接受的答案?有什么问题吗?请纠正我为什么在19小时后,当有一个被接受的答案时,你会重复同样的答案?我不会重复你的答案,我准备了答案但没有提交。除此之外,你的答案是不完整的。在我看来,这两个帐户属于同一个人,不是吗?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
....
@Nullable
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch(position){
case 0:
title = "PAID";
break;
case 1:
title = "UNPAID";
break;
case 2:
title = "ITEM POSTED";
break;
}
return title;
}
....
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
app:tabGravity="center"
android:layout_height="wrap_content">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
SectionsPagerAdapter mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager)
mSectionsPagerAdapter.addFrag(new Paid(),"Paid");
mSectionsPagerAdapter.addFrag(new Unpaid(),"Unpaid");
mSectionsPagerAdapter.addFrag(new ItemPosted(),"ItemPosted");