2

我已经开发了一个 android 应用程序。

在这里,我必须在所有 android 活动上设置标签栏底部。我该怎么做。请给我解决方案。

我总共有 10 个活动,这意味着选项卡栏显示在所有 10 个活动的底部。我该如何在 android 中执行。请帮助我。

这是我的第一个活动:

   setContentView(R.layout.tabbar);
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    TabSpec dbspec = tabHost.newTabSpec("Home");

    dbspec.setIndicator("Home", getResources().getDrawable(R.drawable.home));
    Intent dbIntent = new Intent(this, MainActivity.class);
    dbspec.setContent(dbIntent);
    tabHost.addTab(dbspec);

    TabSpec orderspec = tabHost.newTabSpec("Cart");
    orderspec.setIndicator("Cart", getResources().getDrawable(R.drawable.cart));
    Intent orderIntent = new Intent(this, ViewCartActivity.class);
    orderspec.setContent(orderIntent);
    tabHost.addTab(orderspec);
    TabSpec settingspec = tabHost.newTabSpec("My Account");
    settingspec.setIndicator("My Account", getResources().getDrawable(R.drawable.myaccount));
    Intent settingIntent = new Intent(this, CustomerLogin.class);
    settingspec.setContent(settingIntent);
    tabHost.addTab(settingspec);

标签栏.xml:

<?xml version="1.0" encoding="utf-8"?>
 <TabHost
   android:id="@android:id/tabhost"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

       <RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/linearLayout1"
    android:layout_height="match_parent">

           <TabWidget
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs"
        android:layout_alignParentBottom="true">
          </TabWidget>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/tabcontent">
    </FrameLayout>
    </RelativeLayout>
     </TabHost>

在第一个选项卡中必须执行 MainActivity(GridView) 活动。它被很好地唤醒。在主要活动中我必须单击任何项​​目意味着它是转到 SubCate(listview) 活动。这里我还必须在底部显示标签栏。我怎么能放。

在 subcate.xml 文件中包含以下代码:

<include        
 android:id="@+id/footer"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    layout="@layout/tabbar" />

但标签栏不显示。这里有什么问题。请帮助我。

4

2 回答 2

3

请编写以下代码而不是您的代码以在一个 TabActivity 中添加多个活动,它将解决您的问题。

ActivityStack.java

public class ActivityStack extends ActivityGroup {


private Stack<String> stack;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null)
        stack = new Stack<String>();
    // start default activity
    push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
}

@Override
public void finishFromChild(Activity child) {
    pop();
}

@Override
public void onBackPressed() {
    pop();
}

public void push(String id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        stack.push(id);
        setContentView(window.getDecorView());
    }
}

public void pop() {
    if (stack.size() == 1)
        finish();
    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity(stack.pop(), true);
    if (stack.size() > 0) {
        Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
        Window newWindow = manager.startActivity(stack.peek(), lastIntent);
        setContentView(newWindow.getDecorView());
    }
}
}

TabActivity.java

public class TabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_screen);
        TabHost tabHost = getTabHost();
        Intent intent = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
        spec.setContent(intent);

        tabHost.addTab(spec);

        Intent intent1 = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
        spec1.setContent(intent1);
        tabHost.addTab(spec1);

        tabHost.setCurrentTab(0);
    }
}

第一活动.java

public class FirstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("Tab Sample Activity ");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getParent(), SecondActivity.class);
                ActivityStack activityStack = (ActivityStack) getParent();
                activityStack.push("SecondActivity", intent);
            }
        });
        setContentView(textView);
    }
}

SecondActivity.java

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("First Stack Activity ");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getParent(), ThirdActivity.class);
                ActivityStack activityStack = (ActivityStack) getParent();
                activityStack.push("ThirdActivity", intent);
            }
        });
        setContentView(textView);
    }
}

第三活动.java

public class ThirdActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

将以下 XML 文件添加到您的 res/layout 文件夹中。

1) tab_screen.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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="3dp" >

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</TabHost>

2) main.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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

AndroidManifest.xml:-

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.tabsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityStack"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有关在一个 TabActivity 下添加多个活动的更多信息,请参阅下面的链接,并附上完整的示例。

Android - 一个 TabActivity 下的多个 Android 活动

于 2013-01-02T11:07:56.267 回答
0

您可以使用此类来实现您指定的功能。

import java.util.ArrayList;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

public class TabActivityGroup extends ActivityGroup {

    private ArrayList<String> mIdList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mIdList == null)
            mIdList = new ArrayList<String>();
    }

    /**
     * This is called when a child activity of this one calls its finish method.
     * This implementation calls {@link LocalActivityManager#destroyActivity} on
     * the child activity and starts the previous activity. If the last child
     * activity just called finish(),this activity (the parent), calls finish to
     * finish the entire group.
     */
    @Override
    public void finishFromChild(Activity child) {
        LocalActivityManager manager = getLocalActivityManager();
        int index = mIdList.size() - 1;

        if (index < 1) {
            finish();
            return;
        }

        manager.destroyActivity(mIdList.get(index), true);
        mIdList.remove(index);
        index--;
        String lastId = mIdList.get(index);
        Intent lastIntent = manager.getActivity(lastId).getIntent();
        Window newWindow = manager.startActivity(lastId, lastIntent);
        setContentView(newWindow.getDecorView());
    }

    /**
     * Starts an Activity as a child Activity to this.
     * 
     * @param Id
     *            Unique identifier of the activity to be started.
     * @param intent
     *            The Intent describing the activity to be started.
     * @throws android.content.ActivityNotFoundException.
     */
    public void startChildActivity(String Id, Intent intent) {
        Window window = getLocalActivityManager().startActivity(Id,
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        if (window != null) {
            mIdList.add(Id);
            setContentView(window.getDecorView());
        }
    }

    /**
     * The primary purpose is to prevent systems before
     * android.os.Build.VERSION_CODES.ECLAIR from calling their default
     * KeyEvent.KEYCODE_BACK during onKeyDown.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // preventing default implementation previous to
            // android.os.Build.VERSION_CODES.ECLAIR
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
     * all systems call onBackPressed().
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and
     * add this method.
     */
    @Override
    public void onBackPressed() {
        int length = mIdList.size();
        if (length > 1) {
            Activity current = getLocalActivityManager().getActivity(
                    mIdList.get(length - 1));
            current.finish();
        }
    }
}

通过扩展 TabActivitygroup 创建如下中间活动

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class InterMediateActivity extends TabActivityGroup{
String TabID;
String TabName;
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    TabID=getIntent().getStringExtra("TabID");
    TabName=getIntent().getStringExtra("TabName");
    Log.i("Tab from intermediate",""+TabID+"   "+TabName);
    filterTabs(TabID);
}
private void filterTabs(String TabID)
{
    if(TabID.trim().equals("Home"))
    {       

         startChildActivity("Options", new Intent(this,HomePage.class));

        //TabsUtil.setTabIndicator(specTab,"Wall", null, tabView);
    }
    if(TabID.trim().equals("Menu"))
    {       

         startChildActivity("Options", new Intent(this,MenuPage.class));
        //TabsUtil.setTabIndicator(specTab,"Wall", null, tabView);
   }
    if(TabID.trim().equals("Gallery"))
    {       
        Log.i("GALLERY check", "gallery check");
         startChildActivity("Options", new Intent(this,GalleryPage.class));
    }
    if(TabID.trim().equals("Aboutus"))
    {       

         startChildActivity("Options", new Intent(this,AboutUsPage.class));
    }
    if(TabID.trim().equals("Location"))
    {       

         startChildActivity("Options", new Intent(this,LocationList.class));
    }

    if(TabID.trim().equals("Events"))
    {       
         startChildActivity("Options", new Intent(this,EventsPage.class));
    }
    if(TabID.trim().equals("TipCalculator"))
    {       
         startChildActivity("Options", new Intent(this,TipCalculatorPage.class));
    }
    if(TabID.trim().equals("Special"))
    {       
         startChildActivity("Options", new Intent(this,SpecialPage.class));
    }
    if(TabID.trim().equals("NowRunning"))
    {       
         startChildActivity("Options", new Intent(this,NowRunningPage.class));
    }
    if(TabID.trim().equals("ShowTimes"))
    {       
         startChildActivity("Options", new Intent(this,ShowTimePage.class));
    }
    if(TabID.trim().equals("GpsCoupon"))
    {
         startChildActivity("Options", new Intent(this,GPSCouponPage.class));
    }
    if(TabID.trim().equals("UpcomingMovieNames"))
    {       

        startChildActivity("Options", new Intent(this,UpcomingPage.class));
    }
    if(TabID.trim().equals("PriceListOfServices"))
    {
        startChildActivity("Options", new Intent(this,ServicesPage.class));
    }
    if(TabID.trim().trim().equals("NewsLetter"))
    {
        Log.i("newsletter check", "newsletter check");
        startChildActivity("Options", new Intent(this,NewsLetter.class));
    }
    if(TabID.trim().trim().equals("Website"))
    {
        startChildActivity("Options", new Intent(this,WebSitePage.class));
    }
}
}

而不是直接从 tabactivity 设置选项卡,您可以在中间活动中设置它们。然后从 tabactivity 调用中间活动。

Intent intent = new Intent(this, InterMediateActivity.class);
intent.putExtra("TabID", item.elementAt(0));
intent.putExtra("TabName", item.elementAt(1));
于 2013-01-02T11:31:36.203 回答