【问题标题】:How to default set a tab on starting and set the icon color on selection of a tab如何在开始时默认设置选项卡并在选择选项卡时设置图标颜色
【发布时间】: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 个状态的 selector selecteddefault,然后设置所选项目和默认项目的颜色或背景。然后你只需在标签项 xml 中使用选择器:D
  • 好的,但是如何在开始@lonut J. Bejan 时默认加载标签

标签: android android-tabhost android-tablayout fragment-tab-host


【解决方案1】:

onTabSelected() 方法中添加setColorFilter 方法的代码。它会改变图像的颜色。

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int tabPosition = tab.getPosition();

            if (tabPosition == 0) {
                // active tabName
                imageOne.setColorFilter(getResources().getColor(R.color.redColor));
                // other deactive tabName
                imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
            } else if (tabPosition == 1) {
                // active tabName
                imageTwo.setColorFilter(getResources().getColor(R.color.redColor));
                // other deactive tabName
                imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
            } else if (tabPosition == 2) {
                // active tabName
                imageThree.setColorFilter(getResources().getColor(R.color.redColor));
                // other deactive tabName
                imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
            } else if (tabPosition == 3) {
                // active tabName
                imageFour.setColorFilter(getResources().getColor(R.color.redColor));
                // other deactive tabName
                imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
                imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            int tabPosition = tab.getPosition();
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            int tabPosition = tab.getPosition();
        }
    });

默认选择标签。

如果与 viewPager 一起使用,则

tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewpager);
viewpager.setCurrentItem(0);  // set any number of tab which you want to select by default.

如果不使用 viewPager 则

tabLayout = (TabLayout) findViewById(R.id.tablayout);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);  // which you want to select.
tab.select(); 

希望对您有所帮助。

【讨论】:

  • 如何在开始@Dharmishtha 时默认加载标签
  • 是指将选中的任一选项卡设置为默认?
  • 是的,我需要将标签 2 设置为默认标签 @Dharmishtha
  • 你有没有这样设置viewPager? tabLayout.setupWithViewPager(viewpager);
  • @Martinj 你有没有用viewPager?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 2013-07-24
  • 2020-02-25
  • 1970-01-01
相关资源
最近更新 更多