【问题标题】:How do I change the icon, when a tab is selected X?选择选项卡时如何更改图标 X?
【发布时间】:2010-06-24 17:25:42
【问题描述】:

I have a tabhost with icon, when a tab is selected X, the icon does not appear because the icon is the same color as the selected tab.问题是:

当一个标签被选中时,如何更改图标 X?

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    这就是我所拥有的:

    //TabActivity.onCreate()
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;
    
    intent = new Intent().setClass(this, YourClass.class);
    spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Text",
                getResources().getDrawable(R.drawable.ic_tab_dialer))
                .setContent(intent);
    tabHost.addTab(spec);
    

    然后,您需要将ic_tab_dialer.xml 添加到res/drawable/ 目录中,内容如下:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
            android:state_pressed="false"
            android:drawable="@drawable/ic_tab_selected_dialer" />
        <item android:drawable="@drawable/ic_tab_unselected_dialer" />
    </selector>
    

    我从联系人应用程序 GIT 存储库下载了图标。:

    git://android.git.kernel.org/platform/packages/apps/Contacts.git

    【讨论】:

    • 太棒了!太感谢了。人们知道,如果您的选项卡在单击时不会导致其他活动,则不需要意图,最重要的部分是 getResources().getDrawable(R.drawable.your_xml_file); 我的选项卡中没有文本,也没有使用意图这就像一个魅力。
    • 对我来说很好的解决方案。我正在使用 TabLayout 只需使用正确的可绘制对象设置图标:tab1.setIcon(getResources().getDrawable(R.drawable.ic_tab_one, getTheme()));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多