【问题标题】:Why the Text inside my tabbed activity did not show up?为什么我的选项卡式活动中的文本没有显示?
【发布时间】:2020-01-19 16:18:02
【问题描述】:

我尝试手动创建选项卡式活动。在我的预览 XML 中,它显示如下图所示的文本。

但是当我在手机上运行应用程序时,它没有显示文本。我想知道为什么? 谁能告诉我我做错了哪一部分?

我遵循代码并尝试用自己的代码来做,因为 android 提供的新片段很难理解,所以我决定按照教程手动做,但是当我通过手机运行应用程序时,我发现文字没有出现。我想我在这里遗漏了一些东西,我不太确定我错过了哪一步。如果有人能在我的 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"
            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" />




public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar =  findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        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);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    }

    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_second, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }

    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            Fragment fragment=null;

            switch(position){
                case 0:
                    fragment= new Paid();
                    break;
                case 1:
                    fragment= new Unpaid();
                    break;
                case 2:
                    fragment= new ItemPosted();
                    break;
            }
            return fragment;



        }

        @Override
        public int getCount() {
            return 3;
        }
    }



}

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ItemPosted"></activity>
        <activity android:name=".Unpaid" />
        <activity android:name=".Paid" />
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

【问题讨论】:

    标签: java android xml android-fragments android-fragmentactivity


    【解决方案1】:

    我没有在 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" />
    

    在您的寻呼机适配器中,您可以指定选项卡的名称

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

    【讨论】:

    • 为什么你在 19 小时后重复相同的答案?
    • 我不重复你的答案,我准备答案但不提交
    • 除此之外,您的答案并不完整。在我看来,这两个帐户属于同一个人,不是吗?
    【解决方案2】:

    您必须通过实现getPageTitleSectionsPagerAdapter 设置标题,如下所示:

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

    【讨论】:

    • @lastgg,我的回答有什么问题?为什么您在 19 小时后恢复接受的答案?有什么问题吗?请纠正我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 2021-07-03
    • 2016-01-03
    • 2021-05-15
    • 2015-12-18
    • 1970-01-01
    相关资源
    最近更新 更多