2012-03-29 2 views
0

이 TabHost를 작동 시키려고합니다. XML 레이아웃에 TabHost를 추가하고 프로그램을 실행하면 각 탭마다 서로 다른 내용이 쌓여있는 것을 볼 수 있습니다. 레이아웃이 제대로 작동하고 있음을 알 수 있습니다. 그런 다음 내 스펙 코드를 활동에 추가 한 다음이 화면으로 이동할 때 에뮬레이터에서 프로그램을 닫습니다. 몇 가지 가능성을 염두에 와서TabHost android가 올바르게 작동하지 않습니다.

  welcomeName = (TextView) findViewById(R.id.tWelcome); 
    Test = (TextView) findViewById(R.id.tTest); 
    newTask = (Button) findViewById(R.id.bNewTask); 
    myTask = (ListView) findViewById(R.id.listView); 
    TabHost th = (TabHost) findViewById(R.id.myTabs); 

    welcomeName.setText("Welcome " + ToDoActivity.myUser.getName() + "!"); 

    th.setup(); 
    TabSpec specs = th.newTabSpec("tag1"); 
    specs.setContent(R.id.tab1); 
    specs.setIndicator("All"); 
    th.addTab(specs); 
    specs = th.newTabSpec("tag2"); 
    specs.setContent(R.id.tab2); 
    specs.setIndicator("Personal"); 
    th.addTab(specs); 
+0

: 모든 탭 요소가 제안 된 안드로이드 식별자가 될 수 있도록

특히, 나는 ID를 변경거야? –

답변

0

(중요 유일한 부분 ...) : 여기

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tWelcome" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/bNewTask" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Add Task" /> 

<TabHost 
    android:id="@+id/myTabs" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@+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" > 

       <ListView 
        android:id="@+id/listView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" > 
       </ListView> 
      </LinearLayout> 

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

       <TextView 
        android:id="@+id/tTest" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Test" 
        android:textAppearance="?android:attr/textAppearanceLarge" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

</LinearLayout> 

지금 내 활동 코드 :
그래서 여기 내 XML이다.

TabActivity를 사용하는 경우 (요구 사항이 아니지만 더 쉽게 작업 할 수 있음) 탭 호스트의 ID는 android:id="@android:id/tabhost"이어야합니다.

나는 탭 호스트가 루트 노드 여야한다고도 보았습니다. 나는 안드로이드 워드 프로세서를 사용하여 확인할 수 없습니다. 나는 다른 모든 길들이 실패한 후에 (또는 누군가가 그것이 사실임을 확인한 후에)이 변화를 만들 것입니다.

TabActivity를 사용하는 경우 id로 탭 호스트를 참조하거나 setup() 메소드를 사용할 필요가 없습니다. setup() 메소드를 사용하고있는 것을 보았습니다 만, 액티비티가 TabActivity인지 알 수는 없습니다. 그렇다면 문제가 원인 일 수 있습니다.

다음

내가 탭 레이아웃 설정에 사용하는 방법이며 나를 위해 작동합니다. 탭 호스트는 레이아웃의 루트 노드로 가정하고 id는 android:id/tabhost이며 TabActivity입니다.

tabhost 코드 :이 도움이

public class Tabs extends TabActivity { 

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

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 

     spec = tabHost.newTabSpec("tag1").setIndicator("All").setContent(R.id.tab1); 
     tabHost.addTab(spec); 
     spec = tabHost.newTabSpec("tag2").setIndicator("Personal").setContent(R.id.tab2); 
     tabHost.addTab(spec); 
    } 
} 

희망! 문제가 해결되면 대답을 수락하려면 확인 표시를 클릭하십시오. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

그것은 정보 탭에 대한 요구 사항을 가지고 있으며,이 샘플 코드를 많이 제공합니다

+0

getTabHost()는 내가 정의한 메소드가 아닙니다. 이 메서드는 TabHost Id 또는 뭔가 반환 할 수 있습니까? – user433047

+0

TabActivity를 확장하는 경우이 방법이 있습니다. – Barak

+0

TabActivity는 더 이상 사용되지 않습니다. ActionBarSherlock http://actionbarsherlock.com/을 사용하고 새로운 ActionBar API 만 사용하여 탭을 만듭니다. 이것은 모든 것을 훨씬 쉽게 만들어 특히 아이스크림 샌드위치로 옮겨서, 다음 반년 동안 많은 새로운 장치를 얻을 수 있습니다. – Janusz

0

이 탭의 안드로이드 튜토리얼 페이지입니다. 거기에서 복사하여 붙여 넣기를하고 편집 할 수 있어야합니다. 당신이`대신 TabHost``의 TabLayout`을 사용하는 이유하지

android:id/tabhost 
android:id/tabs 
android:id/tabcontent 
관련 문제