我的应用程序中有一个标签小部件,但每个标签之间没有空格。为了达到这个目的,我添加了这个功能,当我点击一个标签
时就会调用它public void setTabColor(TabHost tabhost) {
_widget = getTabWidget();
for (int i = 0; i < _widget.getChildCount(); i++) {
_widget.getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tabs));
final TextView tv = (TextView) _widget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.BLACK);
LinearLayout.LayoutParams currentLayout =
(LinearLayout.LayoutParams) _widget.getChildAt(i).getLayoutParams();
currentLayout.setMargins(0, 5, 5, 0);
}
//Selected Tab
final TextView tv = (TextView) _widget.getChildAt(tabhost.getCurrentTab()).findViewById(android.R.id.title);
tv.setTextColor(Color.WHITE);
_widget.requestLayout();
}
然后我在我的活动中调用onCreate()末尾的setTabColor()函数,以便选项卡在启动时在它们之间有空格。但是,从onCreate()调用时,空格不会出现,但是在我第一次单击调用SetTabColor()函数的Tab时会出现。
有人可以帮忙吗?
谢谢!
编辑:根据要求,这是我的onCreate()调用setTabColor。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friendhome_activity);
int id = getIntent().getIntExtra("friendID", 0);
_friend = FriendList.getInstance().GetFriendById(id);
final TabHost tabs = getTabHost();
tabs.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
setTabColor(tabs);
}
});
_widget = getTabWidget();
//Friend Overview
Intent overviewIntent = new Intent(this, OverviewActivity.class);
overviewIntent.putExtra("friendID", id);
TabHost.TabSpec overview = tabs.newTabSpec("Overview").setIndicator("Overview").setContent(overviewIntent);
//Messages
Intent messagesIntent = new Intent(this, MessagesActivity.class);
messagesIntent.putExtra("friendID", id);
TabHost.TabSpec messages = tabs.newTabSpec("Messages").setIndicator("Messages ( 0 )").setContent(messagesIntent);
//photos
Intent photosIntent = new Intent(this, PhotosActivity.class);
String tabStr = "Photos " + "( " + _friend.getImageAdapter().getImages().size() + " )";
photosIntent.putExtra("friendID", id);
TabHost.TabSpec photos = tabs.newTabSpec("Photos").setIndicator(tabStr).setContent(photosIntent);
//Videos
Intent videoIntent = new Intent(this, VideosActivity.class);
videoIntent.putExtra("friendID", id);
TabHost.TabSpec videos = tabs.newTabSpec("Videos").setIndicator("Videos ( 0 )").setContent(videoIntent);
tabs.addTab(overview);
tabs.addTab(messages);
tabs.addTab(photos);
tabs.addTab(videos);
setTabColor(tabs);
}