如何编辑Tablayout以使Tab字体相等。我不知道为什么会这样。我从数据库中检索类别,需要将它们显示为选项卡。
您可以在图片上看到问题:Some titles are small
XML:
<LinearLayout 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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".RestaurantMenuActivity">
<androidx.cardview.widget.CardView
// to do
</androidx.cardview.widget.CardView>
<com.google.android.material.tabs.TabLayout
style="@style/MyTabLayout"
android:id="@+id/tabLayout"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp"
app:tabMaxWidth="0dp"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.tabs.TabLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"/>
我按功能动态添加标签Java:
private void addTab(String title) {
tabLayout.addTab(tabLayout.newTab().setText(title));
}
private void getCategories(int restaurantId){
Call<List<Category>> call = apiInterface.getRestaurantCategories(restaurantId);
call.enqueue(new Callback<List<Category>>() {
@Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
categoryList = response.body();
for (Category category:categoryList){
addTab(category.getName());
}
}
@Override
public void onFailure(Call<List<Category>> call, Throwable t) {
}
});
}
编辑
style / MyTabLayout
<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/MyTabTextAppearance</item>
</style>
<style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">16sp</item>
<item name="textAllCaps">true</item>
</style>
我看到XML代码和样式没有问题。我也在模拟器中测试过]
[我在tabLayout.addTab(tabLayout.newTab().setText(title));
中看到问题,因为tablayout将文本推到上方意味着它以两行显示tabText。所以我怀疑标题字符串的末尾有空格。
因此请确保您对标题进行trim,然后再将其传递给Tab。如下所示。
private void addTab(String title) {
tabLayout.addTab(tabLayout.newTab().setText(title.trim()));
}