2014-01-31 54 views
0

我一直在试图创建一个基本的Android应用程序,其中包含标签,点击后会加载一个新的片段 - 每个片段都是一个单独的xml文件。我想使用TabHost而不是活动栏选项卡。我一直在浏览网页上的许多教程,并在这里发布在stackoverflow上,但无济于事。大多数问题和教程正在讨论片段或选项卡,但不是两个主题。预先感谢您的建议。使用TabHost和多个片段创建Android应用程序

我想尝试,它包含一个非常基本的应用程序:

  • 一个片段输入一个数字 - XML file_1(1片),以显示在一个TextView数
  • 一个片段 - XML file_2 (第2片)

回答

0

尝试这样

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

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


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

       <FrameLayout 
        android:id="@+id/tab_1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
      </FrameLayout> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="0dip" 
       android:layout_marginRight="0dip" 
       android:layout_weight="0" /> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

在您的活动的onCreate

mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setOnTabChangedListener(this); 
     mTabHost.setCurrentTab(0); 
     setupTabs(); 

setupTabs()方法

private void setupTabs() { 
     mTabHost.setup(); 
     setupTab(new TextView(this), "Tab1"); 
     setupTab(new TextView(this), "Tab2"); 
      setupTab(new TextView(this), "Tab3"); 
     mTabHost.getTabWidget().setDividerDrawable(R.drawable.empty);//not necessery 

    } 

setupTab()方法//设置一个单一的标签

private void setupTab(final View view, final String tag) { 
     View tabview = createTabView(mTabHost.getContext(), tag); 

     TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
      public View createTabContent(String tag) { 
       return view; 
      } 
     }); 
     mTabHost.addTab(setContent); 

    } 

createTabView方法

private static View createTabView(final Context context, final String text) { 
     int resouceId = R.layout.tabs_bg; 
     if(text.equals("Tab1")) { 
      resouceId = R.layout.tab_1_layout; 
     } else if(text.equals("Tab2")) { 
      resouceId = R.layout.tab_2_layout; 
     } 
     View view = LayoutInflater.from(context).inflate(resouceId, null); 
     return view; 
    } 

onTabChanged方法

@Override 
    public void onTabChanged(String tabId) { 
     Log.d("TAB_CHANGE","onTabChanged(): tabId=" + tabId); 
     updateTab(tabId, R.id.tab_1,new YourFragment()); 
    } 

public void updateTab(String tabId, int placeholder,Fragment fragment) { 
     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction() 
     .replace(placeholder,fragment, tabId) 
     .commit(); 
    } 
+0

感谢您的回复。我会试试这个。几个简单的问题。所有这些方法应该在ActivityMain中吗?ActivityMain会延伸什么?信息如何从一个标签传递到下一个? – gotguts

+0

1.all方法可以在maiactivity本身dd,2.extends FragmentActivity 3.你可以保留一个dataprovider类并保存细节 – deniz

+0

即使在创建必要的xml后,我仍然收到一大堆编译器错误,文件 – gotguts

0

首先,感谢丹尼斯为我安排足够接近正确的解决方案,我可以勉强维持一个工作项目。现在,这应该更接近功能。我将名称更改为通用名称,但希望所有内容都能按预期工作,并且足以帮助您了解正在发生的事情。

TabHostActivity(主活动):

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.TabHost; 
import android.widget.TextView; 

public class TabHostActivity extends FragmentActivity implements TabHost.OnTabChangeListener 
{ 
    private static final String TAG = "TabHostActivity"; 

    private TabHost tabHost; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_tabhost); 

     tabHost = (TabHost) findViewById(android.R.id.tabhost); 
     tabHost.setOnTabChangedListener(this); 
     tabHost.setCurrentTab(0); 
     setupTabs(); 
    } 

    private void setupTabs() 
    { 
     tabHost.setup(); 
     setupTab(new TextView(this), "tab1"); 
     setupTab(new TextView(this), "tab2"); 
     setupTab(new TextView(this), "tab3"); 
     setupTab(new TextView(this), "tab4"); 
    } 

    private void setupTab(final View view, final String tag) 
    { 
     View tabview = createTabView(tabHost.getContext(), tag); 

     TabHost.TabSpec setContent = tabHost.newTabSpec(tag) 
       .setIndicator(tabview) 
       .setContent(new TabHost.TabContentFactory() 
       { 
        public View createTabContent(String tag) 
        { 
         return view; 
        } 
       }); 
     tabHost.addTab(setContent); 
    } 

    /** 
    * Return the view for the individual tabs, the tab view being set is identified by tabId. 
    * 
    * @param context 
    * @param tabId 
    * @return 
    */ 
    private static View createTabView(final Context context, final String tabId) 
    { 
     int resourceId; 
     if (tabId.equals("tab1")) 
     { 
      resourceId = R.layout.tab1; 
     } 
     else if (tabId.equals("tab2")) 
     { 
      resourceId = R.layout.tab2; 
     } 
     else if (tabId.equals("tab3")) 
     { 
      resourceId = R.layout.tab3; 
     } 
     else 
     { 
      resourceId = R.layout.tab4; 
     } 

     return LayoutInflater.from(context).inflate(resourceId, null); 
    } 

    @Override 
    public void onTabChanged(String tabId) 
    { 
     Log.d(TAG, "onTabChanged(): tabId=" + tabId); 

     if (tabId.equalsIgnoreCase("tab1")) 
     { 
      updateTab(android.R.id.tabcontent, new tab1(), tabId); 
     } 
     else if (tabId.equalsIgnoreCase("tab2")) 
     { 
      updateTab(android.R.id.tabcontent, new tab2(), tabId); 
     } 
     else if (tabId.equalsIgnoreCase("tab3")) 
     { 
      updateTab(android.R.id.tabcontent, new tab3(), tabId); 
     } 
     else 
     { 
      updateTab(android.R.id.tabcontent, new tab4(), tabId); 
     } 
    } 

    public void updateTab(int placeholder, Fragment fragment, String tabId) 
    { 
     getSupportFragmentManager().beginTransaction() 
       .replace(placeholder, fragment, tabId) 
       .commit(); 
    } 
} 

activity_tabhost.xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

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


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

       <FrameLayout 
        android:id="@+id/tab_1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@+id/tab_2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@+id/tab_3" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

       <FrameLayout 
        android:id="@+id/tab_4" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 

      </FrameLayout > 

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

     </LinearLayout > 

    </TabHost > 

tab1.xml,tab2.xml,tab3.xml,tab4.xml:

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

</LinearLayout> 

tab1.java,tab2.java,tab3.java,tab4.java:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class tab1 extends Fragment 
{ 
    private static final String TAG = "tab1"; 

    @Override 
    public void onActivityCreated(final Bundle savedInstanceState) 
    { 
     super.onActivityCreated(savedInstanceState); 

     Log.i(TAG, "onActivityCreated"); 
    } 

    @Override 
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) 
    { 
     Log.i(TAG, "onCreateView"); 

     return inflater.inflate(R.layout.tab1, container, false); 
    } 
} 

的AndroidManifest.xml:

<activity android:name="<YOUR_PACKAGE_NAME>.TabHostActivity" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 

</activity> 

这是使用API​​作为目标的V19进行,并使用该android.support.v4.app库最低V10的。

所以,再次感谢deniz让我达到这一点,我花了10个小时尝试一些事情,我发现的一切都过时了。希望这可以帮助其他处于我处于不幸状态的人。你现在应该有一个工作状态,并且可以看到你可以设计标签的位置以及你放置碎片内容的位置。

PS - 是的,在我的泛化传递中,它确实导致了xml内容,并且标签被称为“tab1.whatever”,希望这不是太混乱,这可以通过一些上下文来推断。

相关问题