【发布时间】:2017-12-08 08:53:58
【问题描述】:
我正在使用标签布局和框架布局。
- 我有五个标签。
- 我正在将我的片段加载到此框架布局中。
- 我希望我的第二个选项卡始终默认被选中。
此外,当我选择特定选项卡时,图标应将颜色更改为红色(我使用的是黑色的 png 图标,它们是否可以将它们更改为红色以表明它已被选中)
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/simple_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorWhite"
app:tabIndicatorColor="#f00"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000"
/>
<FrameLayout
android:id="@+id/fl_home"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
这是我的 MainActivity
pb.setIcon(R.drawable.p_icon); // pb is my TabLayout.Tab
mb.setIcon(R.drawable.view_p_icon);
gb.setIcon(R.drawable.c_icon);
ptTab.setIcon(R.drawable.c_icon);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
switch (tab.getPosition()) {
case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new UF()).commit();
break;
case 1:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new DF()).commit();
showAlertDialog("Logout?");
break;
case 2:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
showAlertDialog("Logout?");
break;
case 3:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
showAlertDialog("Logout?");
break;
case 4:
getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
【问题讨论】:
-
是的,当您更改选择选项卡时,您将获得它的位置。然后您可以用红色背景替换图标或更改选项卡选择时图标背景的颜色。\
-
或者,如果您不想以编程方式执行此操作,只需创建一个具有 2 个状态的
selectorselected和default,然后设置所选项目和默认项目的颜色或背景。然后你只需在标签项 xml 中使用选择器:D -
好的,但是如何在开始@lonut J. Bejan 时默认加载标签
标签: android android-tabhost android-tablayout fragment-tab-host