使用索引(仅样式一个选项卡)更改TabLayout中特定选项卡的选定/未选定文本颜色

时间:2017-01-17 16:11:19

标签: android android-tabs android-tablayout spannable text-styling

目前,我正尝试将Tab中的一个TabLayout与其他Tab区别开来。我希望一个具有特定索引的Tab具有红色文本,包括选中和未选中。因为我只要求在特定索引处只有一个app:tabSelectedTextColor="@color/red" app:tabTextColor="@color/red" ,所以下面的常用解决方案不起作用:

Spannable

我还尝试在返回特定Tab的标题时应用@Override public CharSequence getPageTitle(int position) { switch (position) { case TAB_ONE: return getString(R.string.tab_one); case TAB_TWO_RED: Spannable spannable = new SpannableString(getString(R.string.tab_two)); spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, getString(R.string.tab_two).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannable; } return null; } ,但这实际上并未显示:

Tab

如果有人知道如何为仅具有指定索引的public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } public DbSet<User> Users { get; set; } public DbSet<Group> Groups { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=./Blogging.db"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserGroup>().HasKey(x => new { x.UserId, x.GroupId }); modelBuilder.Entity<UserGroup>() .HasOne(pc => pc.User) .WithMany(p => p.UserGroups) .HasForeignKey(pc => pc.UserId); modelBuilder.Entity<UserGroup>() .HasOne(pc => pc.Group) .WithMany(c => c.UserGroups) .HasForeignKey(pc => pc.GroupId); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int MadeBy { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } public class User { [Key] public int Id { get; set; } public ICollection<UserGroup> UserGroups { get; set; } } public class Group { [Key] public int Id { get; set; } public ICollection<UserGroup> UserGroups { get; set; } } // Needed for many-to-many https://stackoverflow.com/questions/29442493/how-to-create-a-many-to-many-relationship-with-latest-nightly-builds-of-ef-core public class UserGroup { [ForeignKey("User")] public int UserId { get; set; } public User User { get; set; } [ForeignKey("Group")] public int GroupId { get; set; } public Group Group { get; set; } } 设置文本样式,我将非常感激。

1 个答案:

答案 0 :(得分:2)

这很简单。

尝试将customView设置为TabLayout中的特定标签,如下所示:

TabLayout tabLayout;
tabLayout = (TabLayout) findViewById(R.id.pTabs);
tabLayout.getTabAt(1).setCustomView(R.layout.tab_two_layout);

您的自定义布局(R.layout.tab_two_layout)的XML看起来与此类似:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_width="60dp"
        android:textColor="@color/red"
        android:layout_gravity="center_vertical"
        android:text="TEXT HERE"
        android:layout_height="wrap_content"/>

</LinearLayout>