android - TabLayout 选项卡标题未在 Android Studio 中显示

标签 android android-studio android-tablayout

选项卡在应用程序中工作,并且显示的选项卡标题正确,即使在我设置了 app:tabTextColor 属性后,设计选项卡也是如此。

我很兴奋,因为添加新代码时我在编辑器中看不到编辑进度。

主类:

  enter code public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create an instance of the tab layout from the view.
        TabLayout tabLayout = findViewById(R.id.tab_layout);
        // Set the text for each tab.
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label1));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label2));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label3));
        // Set the tabs to fill the entire layout.
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        // Use PagerAdapter to manage page views in fragments.
        // Each page is represented by its own fragment.
        final ViewPager viewPager = findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        // Setting a listener for clicks.
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
}

寻呼机适配器:

  public class PagerAdapter extends FragmentStatePagerAdapter {


    private int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumberOfTabs) {
        super(fm);
        this.mNumOfTabs = NumberOfTabs;
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0: return new TabFragment1();
            case 1: return new TabFragment2();
            case 2: return new TabFragment3();
            default: return null;
        }
    }

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

在应用程序中:当我启动应用程序时,一切看起来都像我预期的那样。 enter image description here

XML 文件 主要 Activity 代码。 fragment 文件看起来很好,我没有附加它们。

<RelativeLayout 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabIndicatorColor="@color/design_default_color_primary_dark"
        app:tabTextColor="#eac086"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout" />

</RelativeLayout

Android Studio 看起来像: enter image description here

Gradle 文件:

   android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.lateralnavigation"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

最佳答案

不幸的是,预览编辑器无法识别您在 java/kotlin 代码中编写的内容。它从您的 xml 生成预览,您可以将选项卡直接添加到 xml 中来预览选项卡:

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@color/design_default_color_primary_dark"
    app:tabTextColor="#eac086">
  <android.support.design.widget.TabItem
     android:text="@string/tab_text"/>
  <android.support.design.widget.TabItem
     android:text="@string/tab_text"/>
 <android.support.design.widget.TabItem
     android:text="@string/tab_text"/>
</android.support.design.widget.TabLayout>

然而,这将改变您扩展布局的方式,因为选项卡已经位于 xml 中。不需要做任何事情:

tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label1));

由于该选项卡位于 xml 中,因此您只需为其分配一个 id 并使用 findViewById 即可实际访问该选项卡。如果您有固定数量的选项卡,您甚至可以使用:

tabLayout.getTabAt(position)

访问给定位置的选项卡。

关于android - TabLayout 选项卡标题未在 Android Studio 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388388/

相关文章:

android - 当我使用 TabLayout 时,我的 Android Studio 的应用程序崩溃了,我该如何解决?

android - 使用 tablayout 折叠工具栏

java - android.os.NetworkOnMainThreadException 进入 AsyncTask

Android Studio Gradle 构建非常慢

安卓 :android:configChanges value for Font Style change in Samsung Device

android - 为具有不同 Unity 版本的 Android Studio 导出项目

java - 在 ubuntu 14 上运行 'react-native run-android' 时出错

android - JSON 解析的问题

java - 使用全局变量而不是通过 segue 将值推送到下一个 Activity/ View

android - 如何在选项卡上设置计数?