我試圖創建一個具有2-tabbed活動的應用程序,其中tab1上有一張圖片和一些文本,tab2有兩個按鈕用戶可以點擊(這應該導致進一步的活動)Android Tabs:第二個選項卡中的內容隱藏在第一個選項卡的內容下
由於某種原因,我無法獲得tab2上的按鈕來顯示;我只能看到與tab1相同的圖片。仔細觀察,我發現這些按鈕隱藏在tab1的圖片下。我究竟做錯了什麼?我如何讓這張照片消失,而不是顯示在tab2上?
以下是我的代碼:
IndexActivity
public class IndexActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost.TabSpec spec = getTabHost().newTabSpec("tab1");
spec.setContent(R.id.tab1content);
spec.setIndicator("tab1");
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tab2");
spec.setContent(R.id.tab2content);
spec.setIndicator("tab2");
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
}
我的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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<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">
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
</FrameLayout>
</LinearLayout>
tab1.xml是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/pic"/>
<ScrollView android:id="@+id/scrolltab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tab1content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#65000000"
android:text="@string/text"
android:textColor="#ffffffff"
android:textSize="20sp"
android:padding="15dp"
/>
</ScrollView>
</merge>
TAB2是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab2content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="Button" />
</LinearLayout>
請幫幫忙!我一定是被忽視的東西,但我想不出我什麼sugggest :(
我有點需要這兩個佈局,雖然... – nubicurio
但你試過看看發生了什麼?原因是你多次添加視圖。當您調用'setContentView(R.layout.main)'時,將自動添加'tab1.xml'和'tab2.xml'的佈局,但是您將創建'TabSpecs',其中第一個包含'TextView'(tab1content )來自'tab1.xml',第二個包含'tab2.xml'(tab2content)的整個佈局。當您調用'spec.setContent(...)'時,由於main.xml中的元素,您正手動重複佈局文件內容的通貨膨脹,因此setContentView(R.layout.main)已完成。 –
Squonk
我拿出包含文件,應用程序根本無法啓動(強制關閉);奇怪的是,我似乎通過將android:src更改爲android:background來解決問題。但是你做了什麼很有意義......這是否意味着我實際上有4個選項卡,即使我只看到兩個選項卡?我不知道我在做什麼:( – nubicurio