Open child activity in Tab host android

老子叫甜甜 提交于 2020-01-19 04:05:10

问题


My Tab_Bar.class define define Tabs How I can open child activity

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);

}

}

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"

/>    

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

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


回答1:


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




回答2:


Firstly try using Fragment for creating tabs.

Feature which you are using is deprecated by now.

I have posted complete post over Dynamically changing the fragments inside a fragment tab host?

Please check that out and let me know if you have any concern.

Simply fallow each of defined steps one by one.

CONCEPT

I am putting simple example for clarifying you that exactly how this fragment works.

Take simple scenario of your bed. Your bed consist of several things such as pillow, bed sheet etc. Now consider a case that your bed sheet is dirty. So what you will do by now. You will simply replace that sheet with a new one and then you will have a tight sleep :)

Similarly when you need to change your UI... then this fragment is replaced with a new one.

That's it. Now you are good to go with my post.

Thanks!



来源:https://stackoverflow.com/questions/20988505/open-child-activity-in-tab-host-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!