2013-04-11 12 views
0

下面是我的代碼,它在佈局中添加了選項卡。Android:如何從xml文件調用任何類

public class MyexplistActivity extends ExpandableListActivity { 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tryout); 

     TabHost tabHost=(TabHost)findViewById(R.id.tabHost); 
     tabHost.setup(); 

     TabSpec browsespec=tabHost.newTabSpec("Tab 1"); 
     browsespec.setIndicator("Browse", getResources().getDrawable(R.drawable.icon_browse_tab)); 
     browsespec.setContent(R.id.tab1); 

     TabSpec advspec=tabHost.newTabSpec("Tab 2"); 
     advspec.setIndicator("Advanced Search", getResources().getDrawable(R.drawable.icon_advsrch_tab)); 
     advspec.setContent(R.id.tab2); 

     TabSpec tgspec=tabHost.newTabSpec("Tab 3"); 
     tgspec.setIndicator("Tagged Files", getResources().getDrawable(R.drawable.icon_tgfiles_tab)); 
     tgspec.setContent(R.id.tab3); 

     tabHost.addTab(browsespec); 
     tabHost.addTab(advspec); 
     tabHost.addTab(tgspec); 

     } 
} 

tryout.xml

<?xml version="1.0" encoding="utf-8"?> 

<TabHost android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/tabHost" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 

<TabWidget 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@android:id/tabs" 
/> 

<FrameLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@android:id/tabcontent" 
> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/tab1" 
android:orientation="vertical" 
android:paddingTop="60px" 
> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab1" 
android:id="@+id/txt1" 
/>  

</LinearLayout> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/tab2" 
android:orientation="vertical" 
android:paddingTop="60px" 
> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab 2" 
android:id="@+id/txt2" 
/> 

</LinearLayout> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/tab3" 
android:orientation="vertical" 
android:paddingTop="60px" 
> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab 3" 
android:id="@+id/txt3" 
/> 

</LinearLayout> 
</FrameLayout> 

</TabHost> 

現在我的問題是,我想打電話給另一個類,即annotate.java當我點擊TAB1使背景編碼會。所以什麼程序來完成這樣做?

回答

0

您無法在xml中定義在選中選項卡時會發生什麼情況。你必須用java來做到這一點,你已經在這麼做了。如果Annotate.java是一個Activity,那麼我認爲你必須繼承TabActivity的子類。

值得注意的是,使用TabActivity和TabHost進行導航不再是標準做法(實際上TabActivity已被棄用),而是傾向於使用帶有片段的ActionBar選項卡導航。 Android開發人員網站有關於此的更多信息。

+0

謝謝你的幫助。但我在這個標籤視圖之前嘗試了下面的代碼,並且它完美地工作。但是當我使用相同的代碼來查看標籤時,那麼這些標籤是不可點擊的。 .setIndicator(「Browse」,getResources()。getDrawable(R.drawable.icon_photos_tab)); 意圖browseIntent = new Intent(this,Annotate.class); browsespec.setContent(browseIntent);「##### – Optimus 2013-04-11 04:36:47