gpt4 book ai didi

android - 以编程方式将 TabLayout 中的选项卡文本颜色更改为不同的颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:46 26 4
gpt4 key购买 nike

I have 7 dates tabs in my screen, when tab selected, the text is black in color;而其他可选择的选项卡是白色的。如果日期在另一个月,我希望文本颜色为灰色。

我假设第一个选项卡是 0,第二个选项卡是 1,一直持续到 6。如图所示,我想改变tab(3)、tab(4)、tab(5)和tab(6)的文字颜色。当满足要求的条件(不是 xml)时,如何以编程方式将这 4 个选项卡中的文本颜色设置为灰色?

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">

<TextView
android:id="@+id/lblWeekMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/week"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="@+id/lblWeekNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/lblWeekMsg"
android:layout_alignBaseline="@id/lblWeekMsg"
android:text=""
android:textAppearance="?attr/textAppearanceListItem" />

</RelativeLayout>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar_layout">

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@android:color/white"
app:tabMode="scrollable"
app:tabGravity="fill" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

我用这种方式创建了带有 fragment 的标签

public void setupViewPager(ViewPager viewPager, ArrayList<String> id, ArrayList<String> tasks,
ArrayList<Double> mondayHours, ArrayList<Double> tuesdayHours,
ArrayList<Double> wednesdayHours, ArrayList<Double> thursdayHours,
ArrayList<Double> fridayHours, ArrayList<Double> saturdayHours,
ArrayList<Double> sundayHours) {
Bundle bundle = new Bundle();
bundle.putStringArrayList(EXTRA_CHECKED_TASK_ID, id);
bundle.putStringArrayList(EXTRA_CHECKED_TASKS, tasks);
bundle.putSerializable(EXTRA_MONDAY, mondayHours);
bundle.putSerializable(EXTRA_TUESDAY, tuesdayHours);
bundle.putSerializable(EXTRA_WEDNESDAY, wednesdayHours);
bundle.putSerializable(EXTRA_THURSDAY, thursdayHours);
bundle.putSerializable(EXTRA_FRIDAY, fridayHours);
bundle.putSerializable(EXTRA_SATURDAY, saturdayHours);
bundle.putSerializable(EXTRA_SUNDAY, sundayHours);

final String MON = "MON" + "\n" + MainActivity.sevenDatesList.get(0);
final String TUE = "TUE" + "\n" + MainActivity.sevenDatesList.get(1);
final String WED = "WED" + "\n" + MainActivity.sevenDatesList.get(2);
final String THU = "THU" + "\n" + MainActivity.sevenDatesList.get(3);
final String FRI = "FRI" + "\n" + MainActivity.sevenDatesList.get(4);
final String SAT = "SAT" + "\n" + MainActivity.sevenDatesList.get(5);
final String SUN = "SUN" + "\n" + MainActivity.sevenDatesList.get(6);

adapter = new ViewPagerAdapter(getSupportFragmentManager(), bundle);
adapter.addFragment(new MondayFragment(), MON);
adapter.addFragment(new TuesdayFragment(), TUE);
adapter.addFragment(new WednesdayFragment(), WED);
adapter.addFragment(new ThursdayFragment(), THU);
adapter.addFragment(new FridayFragment(), FRI);
adapter.addFragment(new SaturdayFragment(), SAT);
adapter.addFragment(new SundayFragment(), SUN);

viewPager.setAdapter(adapter);
}

我试过 this ,但我的代码没有使用 tabWidget。我试过 this也是,但我的代码没有使用 tabHost。

我的解决方案基于@Bhavesh Misri 的建议:

ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
//get number of tab
int tabsCount = vg.getChildCount();
//loop the tab
for (int j = 0; j < tabsCount; j++) {
//get view of selected tab
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);

//when the day is not required to display - out of range
if( j<lesserThan || j>largerThan ){
//disable the selected tab
vgTab.setEnabled(false);
//set the not-required tab color transparent ratio
vgTab.setAlpha((float) 0.50);
}
}

最佳答案

试试这个,让我知道这是否适合你:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0) {
tabLayout.getTabAt(0).getIcon().setAlpha(255);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(100);
} else if (tab.getPosition() == 1) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(255);
tabLayout.getTabAt(2).getIcon().setAlpha(100);

} else if (tab.getPosition() == 2) {
tabLayout.getTabAt(0).getIcon().setAlpha(100);
tabLayout.getTabAt(1).getIcon().setAlpha(100);
tabLayout.getTabAt(2).getIcon().setAlpha(255);

}
}

@Surya Prakash Kushawah 你的方式更好。

关于android - 以编程方式将 TabLayout 中的选项卡文本颜色更改为不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40928817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com