Hallo,
für meinen Anwendungsfall sind die Tabs in der ActionBar, die seit Android 3.0 angeboten werden, nicht geeignet. Ich brauche pro Fragment eigene Tabs. Dabei ist wichtig, dass ich nicht auf Support-Libraries zugreifen muss, da die App auf Android 4.+ zugeschnitten sein soll.
Nun habe ich im Internet ein funktionierendes Tab-Beispiel gefunden, das allerdings die Support-Libraries benutzt. Ich würde das gerne auf die neueste API portieren. Allerdings bin ich daran gescheitert.
Das alte Beispiel nutzt FragmentTabHost und FragmentActivity welche aber die Support-Lib benutzen, deswegen wollte ich lediglich TabHost und Fragment verwenden.
Kann mir jemand dabei helfen?
import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;
public class MainActivity extends Activity { // Fragment TabHost as mTabHost private TabHost mTabHost;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// ALTER CODE //mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), Tab1Fragment.class, null); //mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), Tab2Fragment.class, null);
// NEUER CODE mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1").setContent(R.layout.tab1)); mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2").setContent(R.layout.tab2)); }}
import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;
public class Tab1Fragment extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View V = inflater.inflate(R.layout.tab1, container, false);
return V; }}
activity_main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/>
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/>
<FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
</LinearLayout></TabHost>
tab1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".DeviceFragment" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world from tab1!" />
</LinearLayout>
Ich danke im Voraus!
Das beschäftigt mich schon seit mehr als einen Tag!!!
L. G.
Steffo