我需要示例代碼來在android中創建TabHost。 任何人都可以幫助我。任何人都可以給Android TabHost示例代碼?
5
A
回答
7
Android開發人員網站使用TabWidget
和TabHost
,在Android中創建制表符的優秀全功能代碼示例。
+2
是什麼驅使我堅果關於他們的例子嗎?它們不包含所有必要的信息。採取行'mTabHost = getTabHost();'例如 - 沒有事先提及或描述'mTabHost'。而且,他們的另一個選項卡示例有阻止編譯的錯字。而且,當它被編譯時,它就是'核心轉儲'。 – KevinDTimm 2010-02-05 17:49:57
2
我做了關於tabhost相關的代碼,以顯示樂譜
TabHost host = getTabHost();
host.setup();
TabSpec allScoresTab = host.newTabSpec("allTab");
allScoresTab.setIndicator(getResources().getString(R.string.all_scores), getResources().getDrawable(android.R.drawable.star_on));
allScoresTab.setContent(R.id.ScrollViewAllScores);
host.addTab(allScoresTab);
TabSpec friendScoresTab = host.newTabSpec("friendsTab");
friendScoresTab.setIndicator(getResources().getString(R.string.friends_scores), getResources().getDrawable(android.R.drawable.star_on));
friendScoresTab.setContent(R.id.ScrollViewFriendScores);
host.addTab(friendScoresTab);
host.setCurrentTabByTag("allTab");
我的XML包含:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bkgrnd">
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView_Header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/quizicon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:text="@string/scores"
android:textSize="@dimen/screen_title_size"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:layout_gravity="fill_horizontal|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:shadowColor="@android:color/white"
android:textColor="@color/title_color">
</TextView>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView_Header2"
android:layout_height="wrap_content"
android:src="@drawable/quizicon"
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="@+id/ScrollViewAllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="@+id/TableLayout_AllScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
- <ScrollView android:id="@+id/ScrollViewFriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical">
<TableLayout android:id="@+id/TableLayout_FriendScores" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" />
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
0
activity_main.xml中包含
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/scroll" />
<HorizontalScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_alignParentBottom="true" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tabs_bg" />
</HorizontalScrollView>
</RelativeLayout>
</TabHost>
和Java代碼是
TabHost tabHost = getTabHost();
TabSpec spec;
Intent intent;
//Home Tab
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);
intent = new Intent(MainActivity.this, Firstclass.class);
spec = tabHost.newTabSpec("HOME").setIndicator(view1)
.setContent(intent);
tabHost.addTab(spec);
//Calendar Tab
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);
intent = new Intent(MainActivity.this, Calendar.class);
spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
.setContent(intent);
tabHost.addTab(spec);
0
RES /佈局/ activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc916"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 1" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#da8200"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 2" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is tab 3" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
的src/MainActivity.java
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost host = (TabHost)findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("Tab One");
spec.setContent(R.id.tab1);
spec.setIndicator("Tab One");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("Tab Two");
spec.setContent(R.id.tab2);
spec.setIndicator("Tab Two");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("Tab Three");
spec.setContent(R.id.tab3);
spec.setIndicator("Tab Three");
host.addTab(spec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
您可以按照以下教程的幫助
相關問題
- 1. 任何人都可以給我示例代碼_dupenv_s?
- 2. 任何人都可以幫助我與estimote信標示例android代碼開始?
- 3. 任何人都可以給我實時的sqlite示例
- 4. 任何人都可以解釋這個matlab代碼給我嗎?
- 5. 任何人都可以給我的代碼列表視圖?
- 6. Facebook帳號鏈接,任何人都可以給小例子代碼?
- 7. 任何人都可以請在春季自動裝配解釋下面的代碼?任何人都可以請給一個工作示例的鏈接?
- 8. 任何人都有使用切換視圖的示例代碼而不是Android上的TabHost內的活動?
- 9. 任何人都可以給兩個LRU和LFU的例子嗎?
- 10. 任何人都可以演示fl_filename_list?
- 11. 任何人都可以解釋以下代碼的工作...?
- 12. 任何人都可以給我關於基於組件的實體系統在Lua的示例代碼?
- 13. 任何人都可以給Ionic 2框架的InAppBrowser示例應用程序?
- 14. 任何人都可以給我一個JSF2.0 + richfaces4.0的工作示例嗎?
- 15. 任何人都可以共享一個示例.lldbinit文件嗎?
- 16. 任何人都可以用示例解釋string.compare(_:options:range:locale :)嗎?
- 17. 任何人都可以提供Kafka Sink Connector示例Java?
- 18. 任何人都知道我可以如何運行Android示例應用程序?
- 19. 任何人都可以提供示例代碼,通過Android中的藍牙發送CSV文件的值?
- 20. 任何人都可以請給我解釋下面的Java代碼?
- 21. 任何人都可以解釋下面的代碼嗎?
- 22. 任何人都可以解釋我這個代碼行嗎?
- 23. 任何人都可以解釋我這個代碼的工作
- 24. 圖像滑塊代碼,任何人都可以詳細說明
- 25. 任何人都可以請幫我改正這段代碼嗎?
- 26. 任何人都可以幫助我的代碼在ATM?
- 27. 任何人都可以解釋下面的代碼嗎?
- 28. 任何人都可以幫助解釋這部分代碼?
- 29. Javascript:任何人都可以解釋我這段代碼嗎?
- 30. 任何人都可以幫我找到代碼中的錯誤
[此問題]的答案(http://stackoverflow.com/questions/2209406/issues-with-android-tabhost-example)列出了示例代碼的所有問題,以及人們擁有的解決方法找到。 – cdonner 2010-05-19 20:44:48
本教程可以幫助你http://learnncode.wordpress.com/2013/12/18/how-to-use-tabwidget-with-fragments/ – Prachi 2013-12-23 11:45:58