我的应用程序中有一个选项卡小部件,但每个选项卡之间没有空格。为了实现这一点,我添加了这个函数,当我点击一个选项卡时会调用它
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() 函数的选项卡时会出现。
任何人都可以帮忙吗?
谢谢!
编辑:根据要求,这是我的 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);
}