动态更新TabWidget图标?

发布于 2024-09-17 05:28:57 字数 1683 浏览 9 评论 0原文

是否可以更新 TabWidget 图标/指示器?如果是这样,当您通过意图创建 TabContent 时如何?

感谢 @Bart 的编辑,并给出答案

通用代码:

MainActivity:

public class TestActivity extends TabActivity {

public int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
            new Intent(this, OneActivity.class)).setIndicator("One"));
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
            new Intent(this, TwoActivity.class)).setIndicator("Two"));

    changeTitle(0, 20);
}

public void changeTitle(int cnt, int i){
    View v = getTabWidget().getChildAt(cnt);
    TextView tv = (TextView) v.findViewById(android.R.id.title);
    tv.setText("One ("+i+")");
}

}

OneActivity (ChildActivity):

public class OneActivity extends Activity {
TextView tv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);

    tv = (TextView) findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int i = Integer.parseInt((String) tv.getText());
            i++;
            tv.setText(""+i);

             TestActivity parent = OneActivity.this.getParent();
             parent.changeTitle(0,i);

        }
    });
}

}

我想在选项卡标题中显示数字

Is it possible to update the TabWidget icons/indicators? If so, how, when you are creating TabContent through intents?

Edits below with answer thanks to @Bart

Generic code:

MainActivity:

public class TestActivity extends TabActivity {

public int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
            new Intent(this, OneActivity.class)).setIndicator("One"));
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
            new Intent(this, TwoActivity.class)).setIndicator("Two"));

    changeTitle(0, 20);
}

public void changeTitle(int cnt, int i){
    View v = getTabWidget().getChildAt(cnt);
    TextView tv = (TextView) v.findViewById(android.R.id.title);
    tv.setText("One ("+i+")");
}

}

OneActivity (ChildActivity):

public class OneActivity extends Activity {
TextView tv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);

    tv = (TextView) findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int i = Integer.parseInt((String) tv.getText());
            i++;
            tv.setText(""+i);

             TestActivity parent = OneActivity.this.getParent();
             parent.changeTitle(0,i);

        }
    });
}

}

I'd like to show the number in the Tab's title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヅ她的身影、若隐若现 2024-09-24 05:29:02

给定标题和图标的数组

for (int i = 0; i < numTabs; i++ ) {
    TabSpec spec = host.newTabSpec(String.valueOf(i)); //sets tab index
    Drawable icon = res.getDrawable(icons[i]);  //sets the icon
    // set tab text and icon
    spec.setIndicator(titles[i], icon);  //sets the title

    host.addTab(spec);
}

Given a arrays for titles and icons

for (int i = 0; i < numTabs; i++ ) {
    TabSpec spec = host.newTabSpec(String.valueOf(i)); //sets tab index
    Drawable icon = res.getDrawable(icons[i]);  //sets the icon
    // set tab text and icon
    spec.setIndicator(titles[i], icon);  //sets the title

    host.addTab(spec);
}
对你再特殊 2024-09-24 05:29:01

尝试像这样获取 imageView

ImageView v = (ImageView)tabActivity.getTabHost().getTabWidget().getChildTabViewAt(i).findViewById(android.R.id.icon);

v.setImageDrawable(getResources().getDrawable(R.drawable.newIcon));

Try get the imageView like this

ImageView v = (ImageView)tabActivity.getTabHost().getTabWidget().getChildTabViewAt(i).findViewById(android.R.id.icon);

v.setImageDrawable(getResources().getDrawable(R.drawable.newIcon));
要走干脆点 2024-09-24 05:29:00

这是可能的,但有点棘手:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(0);
TextView tv = (TextView) vg.getChildAt(1);

调用 getChildAt() 时应该注意索引。最好的方法始终是调试和检查。

从 1.6 开始,TabWidget 有一个 getChildTabViewAt 方法,借助它,您可以跳过第一行并编写:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildTabViewAt(0);



I attach also source code of Android SDK to show how TabWidget icons are built:

View tabIndicator = inflater.inflate(R.layout.tab_indicator, 
mTabWidget, // tab widget is the parent
false); // no inflate params

final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(mLabel);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); 
iconView.setImageDrawable(mIcon);

正如你所看到的,它包含一个TextView和ImageView。

It is possible but it is a bit of tricky:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(0);
TextView tv = (TextView) vg.getChildAt(1);

You should pay attention to indices while calling getChildAt(). The best way is always to debug and check.

Since 1.6 TabWidget has a method getChildTabViewAt thanks to which you could skip the first line and write:

ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildTabViewAt(0);



I attach also source code of Android SDK to show how TabWidget icons are built:

View tabIndicator = inflater.inflate(R.layout.tab_indicator, 
mTabWidget, // tab widget is the parent
false); // no inflate params

final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(mLabel);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); 
iconView.setImageDrawable(mIcon);

As you can see, it contains a TextView and ImageView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文