2016-04-29 10 views

答えて

0

あなたはその私のために働いて、このコードを試すことができます。

私のXMLファイル:

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

    <TabHost 
     android:id="@+id/tabHost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="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" 
        android:background="#ffc916" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="Android" /> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#da8200" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="Iphone tab" /> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#5b89ff" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="Windows tab" /> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

その私の活動のファイル:

package com.example.sudiproy.tabproject; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class MainActivity extends AppCompatActivity { 

    TabHost tabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TabHost host = (TabHost) findViewById(R.id.tabHost); 
     host.setup(); 

     //Tab 1 
     TabHost.TabSpec spec = host.newTabSpec("Tab One"); 
     spec.setContent(R.id.tab1); 
     spec.setIndicator("Android"); 
     host.addTab(spec); 

     //Tab 2 
     spec = host.newTabSpec("Tab Two"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("Iphone"); 
     host.addTab(spec); 

     //Tab 3 
     spec = host.newTabSpec("Tab Three"); 
     spec.setContent(R.id.tab3); 
     spec.setIndicator("Window"); 
     host.addTab(spec); 


    } 
} 
関連する問題