我想建立一个TabLayout。当我在XML中或编程中添加TabItems时,它们占据空间,可以单击,但它们的文本不显示。当我在OnTabSelectedListener中通过tab.getText()编程访问他们的文本时,它就在那里了。
当我想用传呼机的时候,也发生了同样的事情。
我知道,那
TabItem实际上不是添加到TabLayout中的,它只是一个允许设置选项卡项的文本、图标和自定义布局的虚拟
但我不知道如何在屏幕上显示它的文字。
AndroidStudio的设计显示了预期的布局。
我用Java编写应用程序。
.xml:
<com.google.android.material.tabs.TabLayout
android:id="@+id/exercise_menu_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorSurfaceElevation1dp"
app:tabMode="fixed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:id="@+id/exercise_menu_dayTabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/exercise_menu_myExercisesTabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Exercises"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/exercise_menu_exerciseTabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercises"/>
</com.google.android.material.tabs.TabLayout>
它在AndroidStudio中的外观:
它在应用程序中的外观:
请帮忙材料快把我逼疯了
发布于 2021-02-21 18:42:25
因为我不想使用带有片段的寻呼机,并且无法让TabLayout工作,所以我创建了自己的TabLayout。
我将LinearLayout与TextViews结合使用,只绘制了底部的一行和适当的backgroundColor。它工作得完美无缺,唯一缺少的就是线条运动的动画。
每次单击后,恢复先前选择的textView的外观,并更改新的。
<TextView
android:id="@+id/exercise_menu_dayTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Day"
android:textAlignment="center"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold|italic"
android:textColor="@color/textColor"
android:background="@color/colorSurfaceElevation1dp"
android:paddingVertical="8dp"/>
<TextView
android:id="@+id/exercise_menu_myExercisesTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="My Exercises"
android:textAlignment="center"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold|italic"
android:textColor="?attr/colorPrimary"
android:maxLines="1"
android:background="@drawable/back_line_bottom"
android:paddingVertical="8dp"/>
<TextView
android:id="@+id/exercise_menu_exerciseTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Exercises"
android:textAlignment="center"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold|italic"
android:textColor="@color/textColor"
android:background="@color/colorSurfaceElevation1dp"
android:paddingVertical="8dp"/>
back_line_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/colorSurfaceElevation1dp"/>
</shape>
</item>
</layer-list>
背景变化:
public void changeTypeVisually(int tabId) {
// Resets the background
switch(currentTabId) {
case 0:
myExercisesTextView.setBackgroundColor(getResources().getColor(R.color.colorSurfaceElevation1dp));
myExercisesTextView.setTextColor(getResources().getColor(R.color.textColor));
break;
case 1:
dayTextView.setBackgroundColor(getResources().getColor(R.color.colorSurfaceElevation1dp));
dayTextView.setTextColor(getResources().getColor(R.color.textColor));
break;
case 2:
exerciseTextView.setBackgroundColor(getResources().getColor(R.color.colorSurfaceElevation1dp));
exerciseTextView.setTextColor(getResources().getColor(R.color.textColor));
break;
}
// Adds the underline
switch(tabId) {
case 0:
myExercisesTextView.setBackground(getResources().getDrawable(R.drawable.back_line_bottom));
myExercisesTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
break;
case 1:
dayTextView.setBackground(getResources().getDrawable(R.drawable.back_line_bottom));
dayTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
break;
case 2:
exerciseTextView.setBackground(getResources().getDrawable(R.drawable.back_line_bottom));
exerciseTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
break;
}
currentTabId = tabId;
}
https://stackoverflow.com/questions/66296471
复制相似问题