嗨,我已經在Activity
中添加了兩個TabHost
s,它的工作正常。我添加了另一個帶有ListView
的XML文件,並在我使用Tabhost
的Activity
中調用。它按預期給出錯誤,但我嘗試了很多,但無法解決它。任何人都可以給我任何想法如何管理ListView
和TabHost
都在一個Activity
?我沒有放置代碼,因爲它大部分來自教程。任何幫助,將不勝感激。謝謝。 主要錯誤是「您的內容必須有一個TabHost
其id屬性爲‘android.R.id.tabhost’」列表視圖左側和tabhost右側與20:80屏幕共享比率
5
A
回答
0
樣品活動有標籤和列表視圖與選中的項目:
我貼在下面包含一個TabHost
的樣品活動,其具有3個Tabs
和兩個ListView
,每個tab1
和tab3
中的一個。我使用ArrayList
來初始化我的ArrayAdapter
,從中我可以鏈接我的ListView
。然後,我通過ArrayList
,tab3List
將項目添加到tab3ListView
。請注意,tab3ListView的項目有一個相應的複選框。請注意,現在有些人更喜歡ActionBar.Tabs
而不是TabHost
,但我覺得這種方式更簡單。
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginBottom="-4dp"
android:layout_weight="0" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/tab1List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"></ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tab2Edit1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:hint="Enter text here"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/tab3List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"></ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
主要活動:
public class MainActivity extends Activity{
// TabHost
TabHost th;
LinearLayout tab1, tab2, tab3;
// ListView
ListView tab1ListView, tab3ListView;
// String list
String stringList[] = {"Paris", "Washington", "Amsterdam", "London", "Bale", "Madrid",
"Zurich", "Munich", "Berlin", "Milan", "Rome", "Turin", "Lyon", "Marseille"};
// Adapters
ArrayList<String> tab1List, tab3List;
ArrayAdapter<String> tab1Adapter, tab3Adapter;
// Others
EditText tab2Edit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Initialize Layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize Tabs
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
// Tab1
TabSpec spec1 = th.newTabSpec("tag1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab 1");
th.addTab(spec1);
// Tab2
TabSpec spec2 = th.newTabSpec("tag2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Tab 2");
th.addTab(spec2);
// Tab3
TabSpec spec3 = th.newTabSpec("tag3");
spec3.setContent(R.id.tab3);
spec3.setIndicator("Tab 3");
th.addTab(spec3);
//Initialize Adapter and ListView from Tab1
tab1ListView = (ListView) findViewById(R.id.tab1List);
tab2Edit1 = (EditText) findViewById(R.id.tab2Edit1);
tab1List = new ArrayList<String>(Arrays.asList(stringList));
tab1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tab1List);
tab1ListView.setAdapter(tab1Adapter);
//Initialize Adapter and ListView from Tab3
tab3ListView = (ListView) findViewById(R.id.tab3List);
tab3List = new ArrayList<String>(Arrays.asList(stringList));
tab3Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tab3List);
tab3ListView.setAdapter(tab3Adapter);
tab3ListView.setFocusable(false);
tab3ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// Modify ListView: modify ArrayList and call notifyDataSetChanged() from corresponding adapter
tab3List.add(0,"New item at location 0");
tab3List.add(3,"New item at location 3");
tab3List.add("New item at location end");
tab3Adapter.notifyDataSetChanged();
tab3ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Set what happenss when you check or uncheck an item here
}
});
}
相關問題
- 1. 關閉屏幕導航 - 右側和左側 - 只有css
- 2. 快速檢測屏幕左側和右側的觸摸SKScene
- 3. 如何減少我的屏幕左側和右側的空白?
- 4. 屏幕左側和右側的jquery按鈕?
- 5. HTML:在屏幕的左側和右側有文本區域
- 6. Android屏幕右側的視圖
- 7. 從左側屏幕外部移動圖像到右側屏幕外部Android
- 8. 移動屏幕左側的視圖
- 9. 屏幕左側glTranslatef
- 10. 加入左側和右側
- 11. divs在左側和右側
- 12. CSS左側和右側
- 13. LinearLayout:設計兩個視圖:一個在左側屏幕和一個在右側屏幕
- 14. 左側和右側裁剪圖像
- 15. UIButton在左側和右側的圖像
- 16. iPad:屏幕左側/右側的手勢識別器
- 17. 從引導容器左側向屏幕右側擴展的div
- 18. 檢測屏幕左側/右側部分的觸摸
- 19. 檢測「玩家」是屏幕中間的左側還是右側
- 20. 將左側導向屏幕導航欄移動到屏幕右側的問題
- 21. Android - 右側屏幕按鈕
- 22. 移動視圖左側和右側的額外空間
- 23. 空間出現在滾動視圖android的右側和左側?
- 24. 如何在左側和右側視圖上創建陰影?
- 25. 需要在小屏幕上向右流動UNDER左側(固定寬度右側,左側可變寬度)
- 26. SQL長度,左側,右側新列
- 27. 想要從屏幕的左側到右側顯示滑塊圖像大小
- 28. Java/Android:連續旋轉屏幕左側/右側的水龍頭ImageView圖像
- 29. Android列表項:左側的TextView,右側的圖片
- 30. 左側和右側的導航欄和右側的圖標:右側的圖標被下推
是,錯誤本身是說明你的tabhost必須有id爲 「tabhost」。如果您在佈局文件中提供了不同的ID,它將會崩潰。 – akkilis
是的,我明白了,並把id這樣,我希望我馬上更正android:id =「@ android:id/tabhost」 –