Tab Layout and buttons in Android
I have built an application in android using Tab Layout and it has 2 tabs. I want a different button on each of these tabs. But if I define button in main.xml, I get same button on both the tabs.
I even tried defining the buttons separately in the class of each tab, but was getting some weird errors. can somebody please help me out with this. Below is my code:
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 v开发者_StackOverflow中文版oid 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);
}
}
For 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);
}
}
Tab 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>
I want to have one button along with TextView in iFallApp.java and one button and editText in Settings.java.
For anyone else finding this question:
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);
The setContentView(R.layout.main)
tells the activity what to display on its UI. In this case the view is being set to load from R.layout.main which corresponds to /layout/main.xml (R being the generated lookup class generated by the android framework).
Changing the main.xml would change both tabs because both tabs are contained /within/ the FinalProj
activity inside the TabHost
view defined inside of main.xml
here:
<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">
The whole tab system is described in full detail here: Tab Layout | Android Developers
精彩评论