gpt4 book ai didi

Android,如何在我的代码中的 TabButton 中添加图标

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:42 25 4
gpt4 key购买 nike

我在这里制作了 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;
}
}

最佳答案

维舍什,

如果您查看示例 HERE ...

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

drawable 图标在对 setIndicator() 的调用中传递。您可以用您的 tag 变量替换两个“Albums”字符串。

不过,目前看来您正在为选项卡使用自定义 View 。您只需将 ImageView 添加到布局并将 drawable 设置为您想要的图标。

关于Android,如何在我的代码中的 TabButton 中添加图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6279547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com