[英]What is the proper way to add an icon to a tab to a TabHost for Android?
[英]Add icon to tab in android
我試圖將圖標添加到android中的標簽,但無法正常工作。
這是我的代碼的一部分,我不確定是什么錯誤
th = (TabHost) findViewById(R.id.thbodyview);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.Front);
specs.setIndicator("Front",getResources().getDrawable(R.drawable.tab_one));
th.addTab(specs);
當我運行該應用程序時,該選項卡僅顯示“ Front”,並且沒有圖標,如果有人可以修復它,那就太好了。
謝謝
我知道這個問題是一年多以前提出的,但是無論如何我都會提供一個答案,因為它可能對希望了解這是可能的其他人有所幫助。
以下代碼將使您能夠檢索構成每個選項卡的標題/標題的TextView
和ImageView
(在標准layout/tab_indicator.xml
布局文件中定義):
TabHost tabHost = (TabHost) activity.findViewById(android.R.id.tabhost);
View currentTab = tabHost.getTabWidget().getChildAt(areaIndex);
或獲取正在顯示的當前選項卡的視圖:
View currentTab = tabHost.getCurrentTabView();
然后實際獲取“文本”和“圖像”視圖:
TextView tabTitleField = (TextView) currentTab.findViewById(android.R.id.title);
ImageView tabTitleIcon = (ImageView) currentTab.findViewById(android.R.id.icon);
tabTitleIcon.setImageResource([specify your drawable resource here]);
可以使用以下方式以編程方式將填充添加到ImageView
中:
tabTitleIcon.setPadding(0, 0, 10, 0);在標題和圖標之間增加間距。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.