安卓,如何在我的代码中为TabButton添加图标

3

我已经制作了5个正常工作的选项卡按钮,但现在我想要在每个选项卡上添加图标,请指导/帮助我如何将选项卡图标与选项卡按钮相对应....

我第一次使用stackOverflow,请告诉我是否在发布此代码时错过了任何步骤......谢谢提前。

package com.vishesh.soapbox;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class Start extends TabActivity {
private TabHost tabHost;
//Resources res=getResources();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);
    //textView for signout from application 
    TextView signout=(TextView)findViewById(R.id.signout);
    signout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        finish();   
        }
    });

    // scan button use for read the BarCode through RedLaser
    Button scan=(Button)findViewById(R.id.scan);
    final Intent intent=new Intent(this,RLSample.class);
    scan.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });

    //for Tab button

    tabHost=getTabHost();
    setupTabhost();

    Intent intent1=new Intent().setClass(this, SoapBox.class);
    setupTab(new TextView(this),"SoapBox", intent1);
    Intent intent2=new Intent().setClass(this, Profile.class);
    setupTab(new TextView(this),"Profile", intent2);
    Intent intent3=new Intent().setClass(this, Challenges.class);
    setupTab(new TextView(this),"Challenges", intent3);
    Intent intent4=new Intent().setClass(this, Vault.class);
    setupTab(new TextView(this),"Vault", intent4);
    Intent intent5=new Intent().setClass(this, More.class);
    setupTab(new TextView(this),"More", intent5);
    tabHost.setCurrentTab(0);
    }
    private void setupTabhost()
    {
        tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
    }
    private void setupTab(final View view, final String tag, Intent intent)
    {
        View tabView=createTabView(tabHost.getContext(),tag);
        TabSpec tabSpec=tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
        tabHost.addTab(tabSpec);
    }
    private static View createTabView(final Context context, final String text)
    {
        View view=LayoutInflater.from(context).inflate(R.layout.start_tabs_bg, null);
        TextView textView=(TextView)view.findViewById(R.id.tabsText);
        textView.setText(text);
        return view;
    }
}
1个回答

4

Vishesh,

如果您查看此处的示例HERE...

spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);

在调用setIndicator()时,需要传递drawable图标。同时,你可以使用tag变量来替换"Albums"字符串。
不过,目前看起来你正在使用自定义视图作为选项卡。你只需在布局中添加一个ImageView并将drawable设置为所需的图标即可。

你是否声明了 res?尝试使用 Resources res = getResources(); - Will Tate
不要传递 View,而是传递你的 tag 变量。 - Will Tate
setIndicator(tabView, res.getDrawable(R.drawable.icon)).setContent(intent); 的代码。 - Vishesh Chandra
setIndicator(tag, res.getDrawable(R.drawable.icon)).setContent(intent); 对你不起作用吗? - Will Tate
感谢所有的讨论,请告诉我如何更改选中、按下、未选中时的标签背景颜色,这样这个问题对我来说就很容易了......谢谢。 - Vishesh Chandra
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接