我正在创建带有标签菜单的应用,但是标签标题显示不正确。我尝试重新阅读并再次阅读到目前为止所看过的每篇教程,并将我的代码与其他在stackoverflow上有类似问题的代码进行比较,但我仍然无法确定我在做什么错以及在哪里做错。有人能启发我的错误吗?
谢谢您的澄清。
编辑:我不认为这是颜色。我已经尝试将TabLayout,AppBarLayout和每个选项卡项的颜色和主题分别弄乱(以及完全删除主题),并且标题仍然没有按应有的方式显示。但是我会为我的colors.xml和styles.xml文件发布代码,以防万一。
MainActivity:
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();
}
}
片段类(我有三个类,每个片段一个,它们都有相同的代码,所以我只发布一个):
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);
}
}
页面适配器类:
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.xml中,从appbarlayout()中删除android:theme =“ @ style / AppTheme.AppBarOverlay。我认为是文本颜色问题。它与appbar的颜色相同。因此它没有显示。如果您更改主题,可能您的文字将出现。
用此替换选项卡布局