2014-05-10 7 views
0

J'ai besoin de votre aide pour résoudre un problème simple. Comment dynamiquement "connecter" ou gonfler la mise en page dans le contenu de l'onglet. Je veux un projet avec TabsHost. Pour chaque onglet, j'aime les mises en page différentes. Très simple :) Je crée par défaut projet android:Android gonfler framelayout à l'intérieur du tabcontent

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.tabtest" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.tabtest.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifeste>

étape suivante J'utilise concepteur Eclipse et glisser-déposer dans TabHost activity_main.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

</RelativeLayout> 

étape suivante, j'ajouter un nouveau fichier de mise en page pour les catégories appelées tab1

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/FrameLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

</FrameLayout> 

et catégories classe java

Tous les exemples que j'ai utilisé à partir d'Internet ne pas utiliser concepteur Eclipse pour la définition TabHost, Pourquoi?

Comment dois-je connecter deux mises en page dans le code?

Merci.

+0

voir celui-ci http://developer.android.com/training/implementing-navigation/lateral.html –

+0

Ok, mais il ne TabHost dans cet exemple ?! – user3004288

Répondre

0
// try this way,hope this will help you... 

tab1.xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/txtTab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tab1" 
     android:textSize="25sp"/> 

</LinearLayout> 

tab2.xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/txtTab2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tab2" 
     android:textSize="25sp"/> 

</LinearLayout> 

tab3.xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/txtTab3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tab3" 
     android:textSize="25sp"/> 

</LinearLayout> 

activity_main.xml 
<RelativeLayout 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" 
    tools:context=".MainActivity" > 

    <TabHost 
     android:id="@+id/mytabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </TabWidget> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <include 
        android:id="@+id/tab1" 
        layout="@layout/tab1" > 
       </include> 

       <include 
        android:id="@+id/tab2" 
        layout="@layout/tab2" > 
       </include> 
       <include 
        android:id="@+id/tab3" 
        layout="@layout/tab3" > 
       </include> 

      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</RelativeLayout> 

MainActivity.java 
public class MainActivity extends Activity { 

    private TabHost mytabhost; 
    private TextView txtTab1; 
    private TextView txtTab2; 
    private TextView txtTab3; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mytabhost =(TabHost) findViewById(R.id.mytabhost); 
     txtTab1 =(TextView) findViewById(R.id.txtTab1); 
     txtTab2 =(TextView) findViewById(R.id.txtTab2); 
     txtTab3 =(TextView) findViewById(R.id.txtTab3); 
     mytabhost.setup(); 
     mytabhost.addTab(mytabhost.newTabSpec("Tab1").setIndicator("MyTab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1)); 
     mytabhost.addTab(mytabhost.newTabSpec("Tab2").setIndicator("MyTab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2)); 
     mytabhost.addTab(mytabhost.newTabSpec("Tab3").setIndicator("MyTab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3)); 

     txtTab1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this,"My Tab 1",Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     txtTab2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this,"My Tab 2",Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     txtTab3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this,"My Tab 3",Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

} 
+0

Merci, je vais essayer. – user3004288

Questions connexes