在 Tab 主机 android 中打开子活动 [英] Open child activity in Tab host android

查看:17
本文介绍了在 Tab 主机 android 中打开子活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Tab_Bar.class 定义定义标签我如何打开子活动

我只使用一个 Tab_bar.class 作为 Tab Host

I am using only one single Tab_bar.class for Tab Host

public class Tab_Bar extends TabActivity  {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
    setTabs() ;

}
 void setTabs()
{
    addTab("My Profile", R.drawable.home_normal, MyProfile.class);
    addTab("Search", R.drawable.search_normal, JobSearch.class);

    addTab("Saved Jobs", R.drawable.starred, Saved_jobs.class);
    addTab("Job Alert", R.drawable.job_match, JobAlert.class);
}

private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(1);

}

}

我正在使用这些 xml 文件TabIndicator.xml

I am using these xml file TabIndicator.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"    
android:layout_weight="1"
android:orientation="vertical"

 android:background="@drawable/tab_indicator"
android:padding="5dp">

<ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:src="@drawable/icon"

/> 

 <TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
    style="?android:attr/tabWidgetStyle"
    android:textSize="13sp"

/>    

和 Tab.xml

<?xml version="1.0" encoding="utf-8"?>

<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:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="0dip"
        android:layout_weight="1" />

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

我正在使用 Tab_bar.class 和这些用于 Tab 主机的 xml 文件,但我不知道打开选项卡主机子活动.我是安卓新手.请帮助我,我如何打开儿童活动感谢任何帮助

I am using Tab_bar.class and these xml file for Tab host but i can't have any idea about open tab host child activity . I am new in android. Please Help me, How i can open child activity Any Help is Appreciated

我真的很抱歉我的英语不好

And I am really sorry about my bad English

推荐答案

这并不是您真正需要的,但可能会有所帮助.我使用这个设置来创建动态标签,然后用它们做不同的事情.

This is not eaxctly what you need, but might help. I used this setup to create dynamic tabs and then do different things with them.

protected void onCreate(Bundle savedInstanceState) {
...

    final TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
    Tabs.setup();
    int count;
        for (count =0;count < 2;count++){

            ...

            final int passedTabId = count; 
            NewTab.setContent(new TabHost.TabContentFactory()
            {
                public View createTabContent(String tag)
                {

                   ...

                 RelativeLayout layout = new RelativeLayout(getApplicationContext);
                android.widget.RelativeLayout.LayoutParams params = 
                    new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                layout.setLayoutParams(params);
                layout.setId(some ID);
                layout.setBackgroundResource(R.drawable.room_background);

                   TextView dynText = new TextView(getApplicationContext);
                    ...
                   layout.addView(dynText);


                   return layout;

    // You can set onClickListeners, etc here and then assign them some functions you need
 // You can also create different layouts for every tab according to the passedTabId
                }

            });

            Tabs.addTab(NewTab);
        }
}

问题是,您不能简单地设置另一个活动以在每个选项卡中运行.您需要为您在该选项卡中创建的对象设置一些您需要的功能,但主要活动保持不变.祝你好运:)

Thing is, you cant just simply set another activity to run in each tab. You need to set some functions you need to the objects you create in that tab, but the main activity remains the same. Good luck :)

这篇关于在 Tab 主机 android 中打开子活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆