我已經在android中使用標籤佈局構建了一個應用程序,它有2個選項卡。我希望每個選項卡上都有不同的按鈕。但是,如果我在main.xml中定義按鈕,則在兩個選項卡上都會獲得相同的按鈕。標籤佈局和Android中的按鈕
我甚至試着在每個選項卡的類中分別定義按鈕,但是出現了一些奇怪的錯誤。有人能幫我解決這個問題嗎?下面是我的代碼:
FinalProj.java:
package FinalProj.com;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class FinalProj extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, iFallApp.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
對於TAB1 - iFallApp.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class iFallApp extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the iFall tab");
setContentView(textview);
}
}
選項卡2 - Settings.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Settings extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Settings tab");
setContentView(textview);
}
}
的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"
android:padding="5dp" />
</LinearLayout>
</TabHost>
我想有一個按鈕,在TextView的和iFallApp.java一個按鈕,並在EDITTEXT沿Settings.java。
我解決了這個問題...請不要打擾...謝謝:) – 2011-05-07 03:12:45