2012-11-06 4 views
0

내가이 XML 파일은 다른 XML 파일처럼 나는 각 탭의 작동 방법에다른 XML 파일과 같은 탭을 사용하려면 어떻게해야합니까?

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabHost" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 


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

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


    <LinearLayout 
     android:id="@+id/Sími" 
     android:layout_width="fill_parent" 
     android:layout_height="340dp" 
     android:orientation="vertical" 
     android:paddingTop="60px" > 
    </LinearLayout> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/Sms" 
    android:orientation="vertical" 
    android:paddingTop="60px" 
    > 

    </LinearLayout> 

     <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/Net" 
    android:orientation="vertical" 
    android:paddingTop="60px" 
    > 

    </LinearLayout> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/Greina" 
    android:orientation="vertical" 
    android:paddingTop="60px" 
    > 


    </LinearLayout> 
    </FrameLayout> 

    </TabHost> 

이 코드

package com.example.simgreinirtabs; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class MainActivity extends Activity { 

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

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

     TabSpec spec1=tabHost.newTabSpec("Sími"); 
     spec1.setContent(R.id.Sími); 
     spec1.setIndicator("Sími"); 

     TabSpec spec2=tabHost.newTabSpec("Sms"); 
     spec2.setIndicator("Sms"); 
     spec2.setContent(R.id.Sms); 

     TabSpec spec3=tabHost.newTabSpec("Net"); 
     spec3.setIndicator("Net"); 
     spec3.setContent(R.id.Net); 

     TabSpec spec4=tabHost.newTabSpec("Greina"); 
     spec4.setIndicator("Greina"); 
     spec4.setContent(R.id.Greina); 

     tabHost.addTab(spec1); 
     tabHost.addTab(spec2); 
     tabHost.addTab(spec3); 
     tabHost.addTab(spec4); 
     tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =40; 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

있습니다. 나는 4 개의 탭을 가지고 있고, 4 xml 파일을 가지고있는 것처럼 그들과 함께 작업하고 싶다. 그게 가능하니? 내가 다른 방법으로 할 수 있을까?

답변

1

<include> 태그를 사용해야합니다.

e.e. layout.xml :

 <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/bScan" 
     android:layout_below="@+android:id/tabs" > 

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

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

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

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

    </FrameLayout> 

활동 :

Context ctx = this.getApplicationContext(); 

    TabSpec tabLogin = mTabHost.newTabSpec("login"); 
    tabLogin.setIndicator("login"); 
    tabLogin.setContent(R.id.login_tab); 

    TabSpec tabUser = mTabHost.newTabSpec("user"); 
    tabUser.setIndicator("user"); 
    tabUser.setContent(R.id.user_tab); 

    TabSpec tabRabate = mTabHost.newTabSpec("rabate"); 
    tabRabate.setIndicator("rabate"); 
    tabRabate.setContent(R.id.rabate_tab); 
+0

내가이를두기 전에 내 xml 파일 중 하나를 사용하십니까? Activity 코드를 Oncreate R.layout.activity_main에 넣으시겠습니까? – davidhlynsson

+0

framelayout 요소에 include 태그를 넣어야합니다. 나머지는 onCreate에있을 수 있습니다. 포함 된 요소 내부의 요소에 액세스해야하는 경우. 먼저 v = findViewById (R.id.user_tab)를보아야합니다. 그런 다음 v.findViewById (R.id.element_inside_user_tab); –

+0

나는 당신이 나에게 준 활동 코드를 사용하는 방법을 얻지 못하고있다. 나는 그것을 내 코드 밑에 넣어야하고, 아니면 내 코드를 삭제하고 넣어야한다. Im sry, 나는 일식에서 안드로이드에 꽤 새로왔다. 레이아웃의 이름은 탭 이름과 동일해야합니다. – davidhlynsson

관련 문제