【问题标题】:Add icon to tab in android将图标添加到android中的选项卡
【发布时间】:2014-07-15 18:26:19
【问题描述】:

我正在尝试将图标添加到 android 中的选项卡,但它不起作用。

这是我的代码的一部分,我不确定是什么问题

th = (TabHost) findViewById(R.id.thbodyview);
th.setup();
    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.Front);
    specs.setIndicator("Front",getResources().getDrawable(R.drawable.tab_one));
    th.addTab(specs);

当我运行应用程序时,选项卡只显示“前面”并且没有图标,如果有人可以修复它,那就太好了。

谢谢

【问题讨论】:

  • 你的drawable是多少px尺寸

标签: android android-tabhost android-tabs


【解决方案1】:

我知道这个问题是在一年前提出的,但无论如何我都会提供一个答案,因为它可能对其他想知道这如何可能的人有所帮助。

以下代码将使您能够检索构成每个选项卡的标题/标题的TextViewImageView(在标准layout/tab_indicator.xml 布局文件中定义):

    TabHost tabHost = (TabHost) activity.findViewById(android.R.id.tabhost);
    View currentTab = tabHost.getTabWidget().getChildAt(areaIndex);

或获取当前选项卡的视图:

    View currentTab = tabHost.getCurrentTabView();

然后实际获取 Text 和 Image 视图:

    TextView tabTitleField = (TextView) currentTab.findViewById(android.R.id.title);

    ImageView tabTitleIcon = (ImageView) currentTab.findViewById(android.R.id.icon);
    tabTitleIcon.setImageResource([specify your drawable resource here]);

可以使用

以编程方式将填充添加到 ImageView
tabTitleIcon.setPadding(0, 0, 10, 0);
在标题和图标之间添加间距。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多