gpt4 book ai didi

android - 在使用带有选项卡布局的自定义 View 时加载 Activity 时突出显示 View 寻呼机中的第一项?

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:13 25 4
gpt4 key购买 nike

我正在使用像 youtube 应用程序这样的选项卡,并且想突出显示加载 Activity 的第一页,所以我使用了以下代码

viewpager.setCurrentItem(0);//which does not highlight first tab onstarting the tab activity

我使用 drawable 作为 tablayout 中图标的背景。我通过获取 tablayout 的每个子项来设置 fragment 创建时的可绘制背景。我在

中设置了文字颜色
<item name="tabTextAppearance">@style/TabTextAppearance</item>

</style>
<style name="TabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">@color/tab_text_selector</item>
<item name="android:textAllCaps">false</item>
</style>

选择器文件之一

< ?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/favorite_selected" />
<item android:state_focused="true" android:drawable="@drawable/favorite_selected" />
<item android:state_pressed="true" android:drawable="@drawable/favorite_selected" />
<item android:drawable="@drawable/favorites" />
</selector>

还有另外两个可绘制对象,我正在为选项卡使用自定义布局当应用程序在 View 寻呼机中加载时,我如何显示第一个突出显示的项目

最佳答案

这有点棘手。

enter image description here

你应该这样设置背景:

<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabLayout"
app:tabBackground="@drawable/tab_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

即将 android:background 更改为 app:tabBackground

现在,一旦您按下选项卡 - 它就会获得适当的背景 drawable

将选择器应用于 TabLayout 中的图标:

tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_i_selector);
}

更新:

一来二去,我们和@Preethi一起解决了这个问题:

 tabLayout.getTabAt(0).getCustomView().setSelected(true);

重要说明。 @Preethi 正在使用 setCustomView() 方法为 View 设置 制表符

另一种可能的解决方法是避免自定义 View 并以这种方式设置 TextColor&Icons:

    tabLayout.setTabTextColors(getColor(R.color.normal_color), getColor(R.color.selected_color));
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_selector);
}

关于android - 在使用带有选项卡布局的自定义 View 时加载 Activity 时突出显示 View 寻呼机中的第一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400211/

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