向选项卡宿主添加片段

4

如何在Android中使用tabhost控制片段。我有一个活动和四个片段以及它们对应的xml文件。我将我的选项卡主机添加到main_layout_activity.xml文件中。那里我有另一个Linnerlayout,我想从Fragement替换此linnerlayout。要替换这些Fragements,我必须使用tabhost。请帮帮我?我找到了一个代码,但它很混乱。请看下面的代码。

final TabHost tabs= (TabHost) findViewById(R.id.tabhost);
  tabs.setup();

  TabHost.TabSpec spec=tabs.newTabSpec("tag1");
  spec.setContent(R.id.fragment1);
  spec.setIndicator("View Raves");
  tabs.addTab(spec);
  spec=tabs.newTabSpec("tag2");
  spec.setContent(R.id.fragment2);
  spec.setIndicator("My Badges");
  tabs.addTab(spec);
  tabs.setCurrentTab(0); 

在有很多想法之后,我已经按照以下方式纠正了我的代码。请查看以下内容:

MainActivity.java

package com.example.testtabs;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;




public class MainActivity extends TabActivity  {


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


  setContentView(R.layout.activity_main);


     FragementTest testfrag=new FragementTest();
  FragmentManager manager=getFragmentManager();
   FragmentTransaction transacti
  transaction.add(R.id.my_layout1, testfrag, "");
  transaction.commit();


    tab= (TabHost) findViewById(android.R.id.tabhost);
    tab.setup();

 }

}

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"
    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:c 
    android:id="@+id/my_layout"

    >

    <LinearLayout
        android:id="@+id/my_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@android:id/tabhost"
        android:layout_marginTop="85dp"
         android:orientati >

    </LinearLayout>

    <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:orientati >

            <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" >


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

</RelativeLayout>
1个回答

4
创建一个方法,像这样,并将您的选项卡数据传递为其视图,名称和片段类。
/**
 * Used to create a tab
 * 
 * @param view
 * @param tag
 * @param name
 */
private void setupTab(final View view, final String tag, Class<?> name) {
        View tabview = createTabView(this, tag);
        TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview)
            .setContent(new TabContentFactory() {
                public View createTabContent(String tag) {
                    return view;
                }
            });
    tabHost.addTab(setContent, name, null);
}



/**
 * Used to create tabview object and setting parameters
 * 
 * @param context
 * @param text
 * @return
 */
private View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout,
            null);
    Button _button = (Button) view.findViewById(R.id.tabText);
    _button.setText(text);
    return view;
}

而且像这样调用该方法。
setupTab(new Button(this), "Tab1", Fragment1.class);
setupTab(new Button(this), "Tab2", Fragment2.class);

我想使用final TabHost tabs= (TabHost) findViewById(R.id.tabhost); 但是我的xml文件中写的是"android:id="@android:id/tabhost",这样怎么看得到呢?我无法初始化tab host,有什么想法吗? - VenushkaT
你可以通过以下方式获取TabHost对象:tabHost = (TabHost) findViewById(android.R.id.tabhost); - kalyan pvs
请在上面找到我编辑过的代码,我已经发布了我的所有代码。现在我正在使用我的代码,并尝试添加您的代码。非常感谢。 - VenushkaT
这是关于使用FragmentTabHost的选项卡的内容,具体请参考https://github.com/colaboy2004/FragmentTabHostExample。 - kalyan pvs
那个人使用了ActionBar,而我需要使用Tabhost。很抱歉我没有帮上忙。无论如何,谢谢你。 - VenushkaT
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接