TabLayout 上的标签标题未显示
Tabs titles on TabLayout not showing
更新:我将适配器 class 修改为以下内容,现在可以正常工作了:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
R.string.tab_text_2, R.string.tab_text_3};
//private final Context mContext;
private static int tab_count;
private Context mContext;
public SectionsPagerAdapter(FragmentManager fm, int tab_count, Context context) {
super(fm);
mContext = context;
this.tab_count = tab_count;
}
@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).
//return PlaceholderFragment.newInstance(position + 1);
switch (position)
{
case 0:
Tab1Pagina_Inicial tab1 = new Tab1Pagina_Inicial();
return tab1;
case 1:
Tab2Apartamentos tab2 = new Tab2Apartamentos();
return tab2;
case 2:
Tab3ItensDeApartamentos tab3 = new Tab3ItensDeApartamentos();
return tab3;
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return mContext.getString(R.string.tab_text_1);
case 1:
return mContext.getString(R.string.tab_text_2);
case 2:
return mContext.getString(R.string.tab_text_3);
default:
return null;
}
}
@Override
public int getCount()
{
return tab_count;
}
}
添加 getPageTitle 成功了!
感谢您的小费。 ^^
我正在创建一个带有选项卡菜单的应用程序,但选项卡的标题显示不正确。
我已经尝试重新阅读并再次查看我到目前为止看到的每一个教程,并将我的代码与其他人在 Whosebug 上有类似问题的地方进行比较,但我似乎仍然无法确定我做错了什么以及哪里做错了。谁能告诉我我的错误?
提前感谢您的澄清。
编辑:我不认为这是颜色。我试过分别弄乱 TabLayout、AppBarLayout 和每个选项卡项的颜色和主题(以及完全删除主题),但标题仍然没有按应有的方式显示。但我会 post 我的 colors.xml 和 styles.xml 文件的代码以防万一。
编辑 2:伙计们,我发现了一些奇怪的东西。现在我不确定我是否真的像我想的那样理解 TabLayout。
在我使用 setupWithViewPager 方法设置我的 TabLayout 和我的 ViewPager 之前,一切都按预期显示。我的意思是标题出现了。但我实际上只能通过滑动来更改标签。单击每个选项卡什么都不做。
我会附上一张图片来说明我的意思。
以下是我自以为理解的内容:
我知道我需要一个适配器来将每个片段呈现为每个选项卡的内容。
我知道 ViewPager 是负责在选项卡之间滑动的组件。
TabLayout 是我可以在其中显示选项卡的布局。
我需要将我的 TabLayout 连接到我的 ViewPager,以便在我单击特定选项卡时显示正确的内容。
我是不是漏掉了什么?
主要活动:
package com.example.pousadaviladascores.Activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.example.pousadaviladascores.DAO.SqlAccess;
import com.example.pousadaviladascores.UI.SectionsPagerAdapter;
import com.example.pousadaviladascores.R;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
SqlAccess sqlAccess;
//Declaring TabLayout and ViewPager
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
sqlAccess = new SqlAccess(this);
//Initializing TabLayout
tabLayout = findViewById(R.id.tabLayout);
//Initializing viewPager
viewPager = findViewById(R.id.view_pager);
//Initializing page adapter
//OBS: In Android, Adapter is a bridge between UI component and data source that helps us to fill data in UI component.
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
//Sets a PagerAdapter that will supply views for this pager as needed
viewPager.setAdapter(sectionsPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onDestroy() {
sqlAccess.getDbHelper().close();
super.onDestroy();
}
}
片段class(我有三个class,每个片段一个,它们都有相同的代码,所以我只post一个):
package com.example.pousadaviladascores.Activities;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.example.pousadaviladascores.R;
public class Tab1Pagina_Inicial extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab1_fragment_pagina_inicial, container, false);
}
}
页面适配器class:
package com.example.pousadaviladascores.UI;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.pousadaviladascores.Activities.Tab1Pagina_Inicial;
import com.example.pousadaviladascores.Activities.Tab2Apartamentos;
import com.example.pousadaviladascores.Activities.Tab3ItensDeApartamentos;
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static int tab_count;
public SectionsPagerAdapter(FragmentManager fm, int tab_count) {
super(fm);
//mContext = context;
this.tab_count = tab_count;
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1Pagina_Inicial tab1 = new Tab1Pagina_Inicial();
return tab1;
case 1:
Tab2Apartamentos tab2 = new Tab2Apartamentos();
return tab2;
case 2:
Tab3ItensDeApartamentos tab3 = new Tab3ItensDeApartamentos();
return tab3;
default:
return null;
}
}
@Override
public int getCount()
{
return tab_count;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingRight="20dp"
android:text="@string/pousada_name"
android:textSize="24sp" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="34dp"
android:theme="?attr/actionBarTheme"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
选项卡布局(同样,除文本外其他都一样):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textTab"
android:layout_width="369dp"
android:layout_height="54dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="27dp"
android:layout_marginLeft="27dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:text="@string/tab_text_1"
android:textSize="30sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" />
</RelativeLayout>
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
为了更好地说明我的意思,下面是 activity_main 的设计及其外观:
实际情况如下:
每页的文本都显示正常,但标题不是。 :/
Activity 主要
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingRight="20dp"
android:text="@string/pousada_name"
android:textSize="24sp" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="34dp"
android:theme="?attr/actionBarTheme"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
你的主要activity
public class MainActivity extends AppCompatActivity {
SqlAccess sqlAccess;
//Declaring TabLayout and ViewPager
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
sqlAccess = new SqlAccess(this);
viewPager = (ViewPager) findViewById(R.id.view_pager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Pagina_Inicial(), "ONE");
adapter.addFragment(new Tab2Apartamentos(), "TWO");
adapter.addFragment(new Tab3ItensDeApartamentos(), "THREE");
viewPager.setAdapter(adapter);
}
@Override
protected void onDestroy() {
sqlAccess.getDbHelper().close();
super.onDestroy();
}
}
并用这个替换您的部分寻呼机适配器
class SectionPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
在 activity_main.xml
中,从 appbarlayout()
中删除 android:theme="@style/AppTheme.AppBarOverlay
。我认为文本颜色问题。它与 appbar
颜色相同。所以它没有显示。如果您更改主题,您的文字可能会出现。
有 2 个问题。
方法 tabLayout.setupWithViewPager()
删除所有选项卡,然后使用以下方法添加改编的选项卡:addTab(newTab().setText(pagerAdapter.getPageTitle(i)), false);
您必须在您的适配器中覆盖 方法getPageTitle
以获取标题字符串。默认值为 null
。类似于:
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "....";
case 1:
return "....";
....
}
return null;
}
还要使用 com.google.android.material.tabs.TabLayout
,您必须在您的应用中使用 Material Components Theme。您正在使用 AppCompat 主题。
您可以:
- 切换到 Material 组件主题(在下一版本中建议和要求)
- 添加
app:tabTextColor
属性:
<com.google.android.material.tabs.TabLayout
app:tabTextColor="@color/mySelector"
.../>
- 定义样式:
<com.google.android.material.tabs.TabLayout
style="@style/Widget.MaterialComponents.TabLayout"
TabLayout 在 Material 组件主题中使用的默认样式是 Widget.MaterialComponents.TabLayout
,而在 AppCompat 中是 Widget.Design.TabLayout
有一些不同,在你的情况下是问题所在:
<style name="Widget.Design.TabLayout" >
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabTextColor">@null</item>
更新:我将适配器 class 修改为以下内容,现在可以正常工作了:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
R.string.tab_text_2, R.string.tab_text_3};
//private final Context mContext;
private static int tab_count;
private Context mContext;
public SectionsPagerAdapter(FragmentManager fm, int tab_count, Context context) {
super(fm);
mContext = context;
this.tab_count = tab_count;
}
@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).
//return PlaceholderFragment.newInstance(position + 1);
switch (position)
{
case 0:
Tab1Pagina_Inicial tab1 = new Tab1Pagina_Inicial();
return tab1;
case 1:
Tab2Apartamentos tab2 = new Tab2Apartamentos();
return tab2;
case 2:
Tab3ItensDeApartamentos tab3 = new Tab3ItensDeApartamentos();
return tab3;
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return mContext.getString(R.string.tab_text_1);
case 1:
return mContext.getString(R.string.tab_text_2);
case 2:
return mContext.getString(R.string.tab_text_3);
default:
return null;
}
}
@Override
public int getCount()
{
return tab_count;
}
}
添加 getPageTitle 成功了! 感谢您的小费。 ^^
我正在创建一个带有选项卡菜单的应用程序,但选项卡的标题显示不正确。 我已经尝试重新阅读并再次查看我到目前为止看到的每一个教程,并将我的代码与其他人在 Whosebug 上有类似问题的地方进行比较,但我似乎仍然无法确定我做错了什么以及哪里做错了。谁能告诉我我的错误?
提前感谢您的澄清。
编辑:我不认为这是颜色。我试过分别弄乱 TabLayout、AppBarLayout 和每个选项卡项的颜色和主题(以及完全删除主题),但标题仍然没有按应有的方式显示。但我会 post 我的 colors.xml 和 styles.xml 文件的代码以防万一。
编辑 2:伙计们,我发现了一些奇怪的东西。现在我不确定我是否真的像我想的那样理解 TabLayout。 在我使用 setupWithViewPager 方法设置我的 TabLayout 和我的 ViewPager 之前,一切都按预期显示。我的意思是标题出现了。但我实际上只能通过滑动来更改标签。单击每个选项卡什么都不做。 我会附上一张图片来说明我的意思。
以下是我自以为理解的内容: 我知道我需要一个适配器来将每个片段呈现为每个选项卡的内容。 我知道 ViewPager 是负责在选项卡之间滑动的组件。 TabLayout 是我可以在其中显示选项卡的布局。 我需要将我的 TabLayout 连接到我的 ViewPager,以便在我单击特定选项卡时显示正确的内容。
我是不是漏掉了什么?
主要活动:
package com.example.pousadaviladascores.Activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.example.pousadaviladascores.DAO.SqlAccess;
import com.example.pousadaviladascores.UI.SectionsPagerAdapter;
import com.example.pousadaviladascores.R;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
SqlAccess sqlAccess;
//Declaring TabLayout and ViewPager
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
sqlAccess = new SqlAccess(this);
//Initializing TabLayout
tabLayout = findViewById(R.id.tabLayout);
//Initializing viewPager
viewPager = findViewById(R.id.view_pager);
//Initializing page adapter
//OBS: In Android, Adapter is a bridge between UI component and data source that helps us to fill data in UI component.
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
//Sets a PagerAdapter that will supply views for this pager as needed
viewPager.setAdapter(sectionsPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onDestroy() {
sqlAccess.getDbHelper().close();
super.onDestroy();
}
}
片段class(我有三个class,每个片段一个,它们都有相同的代码,所以我只post一个):
package com.example.pousadaviladascores.Activities;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.example.pousadaviladascores.R;
public class Tab1Pagina_Inicial extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab1_fragment_pagina_inicial, container, false);
}
}
页面适配器class:
package com.example.pousadaviladascores.UI;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.pousadaviladascores.Activities.Tab1Pagina_Inicial;
import com.example.pousadaviladascores.Activities.Tab2Apartamentos;
import com.example.pousadaviladascores.Activities.Tab3ItensDeApartamentos;
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static int tab_count;
public SectionsPagerAdapter(FragmentManager fm, int tab_count) {
super(fm);
//mContext = context;
this.tab_count = tab_count;
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1Pagina_Inicial tab1 = new Tab1Pagina_Inicial();
return tab1;
case 1:
Tab2Apartamentos tab2 = new Tab2Apartamentos();
return tab2;
case 2:
Tab3ItensDeApartamentos tab3 = new Tab3ItensDeApartamentos();
return tab3;
default:
return null;
}
}
@Override
public int getCount()
{
return tab_count;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingRight="20dp"
android:text="@string/pousada_name"
android:textSize="24sp" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="34dp"
android:theme="?attr/actionBarTheme"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
选项卡布局(同样,除文本外其他都一样):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textTab"
android:layout_width="369dp"
android:layout_height="54dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="27dp"
android:layout_marginLeft="27dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:text="@string/tab_text_1"
android:textSize="30sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" />
</RelativeLayout>
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
为了更好地说明我的意思,下面是 activity_main 的设计及其外观:
实际情况如下:
每页的文本都显示正常,但标题不是。 :/
Activity 主要
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingRight="20dp"
android:text="@string/pousada_name"
android:textSize="24sp" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="34dp"
android:theme="?attr/actionBarTheme"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
你的主要activity
public class MainActivity extends AppCompatActivity {
SqlAccess sqlAccess;
//Declaring TabLayout and ViewPager
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
sqlAccess = new SqlAccess(this);
viewPager = (ViewPager) findViewById(R.id.view_pager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Pagina_Inicial(), "ONE");
adapter.addFragment(new Tab2Apartamentos(), "TWO");
adapter.addFragment(new Tab3ItensDeApartamentos(), "THREE");
viewPager.setAdapter(adapter);
}
@Override
protected void onDestroy() {
sqlAccess.getDbHelper().close();
super.onDestroy();
}
}
并用这个替换您的部分寻呼机适配器
class SectionPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
在 activity_main.xml
中,从 appbarlayout()
中删除 android:theme="@style/AppTheme.AppBarOverlay
。我认为文本颜色问题。它与 appbar
颜色相同。所以它没有显示。如果您更改主题,您的文字可能会出现。
有 2 个问题。
方法 tabLayout.setupWithViewPager()
删除所有选项卡,然后使用以下方法添加改编的选项卡:addTab(newTab().setText(pagerAdapter.getPageTitle(i)), false);
您必须在您的适配器中覆盖 方法getPageTitle
以获取标题字符串。默认值为 null
。类似于:
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "....";
case 1:
return "....";
....
}
return null;
}
还要使用 com.google.android.material.tabs.TabLayout
,您必须在您的应用中使用 Material Components Theme。您正在使用 AppCompat 主题。
您可以:
- 切换到 Material 组件主题(在下一版本中建议和要求)
- 添加
app:tabTextColor
属性:
<com.google.android.material.tabs.TabLayout
app:tabTextColor="@color/mySelector"
.../>
- 定义样式:
<com.google.android.material.tabs.TabLayout
style="@style/Widget.MaterialComponents.TabLayout"
TabLayout 在 Material 组件主题中使用的默认样式是 Widget.MaterialComponents.TabLayout
,而在 AppCompat 中是 Widget.Design.TabLayout
有一些不同,在你的情况下是问题所在:
<style name="Widget.Design.TabLayout" >
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabTextColor">@null</item>