hej, har följt guiden från google att göra tabbar i appen.
Och allt fungerar som det ska, jag får tabbar det skrivs olika text för varje tab.
Men hur lägger jag till mer innehåll till tabcontent. som det är nu så läggs en text till på varje sida som visar t.ex. "tab1" och detta görs i koden tab1.java och inte i t.ex. en tab1.xml. hur får jag en specifik tab att använda en specifik xml för layout? att lägga till via koden verkar inte fungera då jag lägger till en bild så ersätter? den texten.
har provat så här i tab-koden, och då försvinner texten och bilden visas i mitten på sidan, hur styr man det?
Kod:
TextView textView = new TextView(this);
textView.setText("text1");
setContentView(textView);
ImageView imgPres = new ImageView(this);
imgPres.setImageResource(R.drawable.bild);
imgPres.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
imgPres.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(imgPres);
Här följer koden som jag använder.
Huvudkoden:
Kod:
public class vader extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** använder inte main layouten utan kör med tab.xml */
setContentView(R.layout.tab);
/** TabHost will have Tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
/** TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */
//LayoutInflater.from(this).inflate(R.layout.tabweather, tabHost.getTabContentView(),true);
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec TabSpecWeather = tabHost.newTabSpec("tid1");
TabSpec TabSpecPlaces = tabHost.newTabSpec("tid2");
TabSpec TabSpecPlus = tabHost.newTabSpec("tid3");
TabSpec TabSpecSettings = tabHost.newTabSpec("tid4");
/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
TabSpec1.setIndicator("tab1", getResources().getDrawable(R.drawable.ic_tab1));
TabSpec2.setIndicator("tab2", getResources().getDrawable(R.drawable.ic_tab2));
TabSpec3.setIndicator("tab3", getResources().getDrawable(R.drawable.ic_tab3));
TabSpec4.setIndicator("tab4", getResources().getDrawable(R.drawable.ic_tab4));
//TabSpecWeather.setContent(R.id.linearLayout2);
TabSpec1.setContent(new Intent(this,Tab1.class));
TabSpec2.setContent(new Intent(this,Tab2.class));
TabSpec3.setContent(new Intent(this,Tab3.class));
TabSpec4.setContent(new Intent(this,Tab4.class));
/** Add tabSpec to the TabHost to display*/
tabHost.addTab(TabSpec1);
tabHost.addTab(TabSpec2);
tabHost.addTab(TabSpec3);
tabHost.addTab(TabSpec4);
}
}
Koden för första tabben tab1.class
Kod:
public class Tab1 extends Activity {
/** Call when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("tab1");
setContentView(textView);
}
}
layout för appen tab.xml
HTML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="25sp"
android:src="@drawable/title_logo"
android:background="#000000"/>
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost">
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</RelativeLayout>
</TabHost>
</LinearLayout>