Custom Tabwiget for Android

后端 未结 2 1190
名媛妹妹
名媛妹妹 2021-01-26 10:01

I have designed an activity which I\'m using for tab(Custom Global activity),this activity has different buttons like tab and clicking on buttons calling corresponding activity

2条回答
  •  孤街浪徒
    2021-01-26 11:04

    Please write below code instead of your code for add multiple activities in one TabActivity, it will solve your problem.

    ActivityStack.java

    private Stack stack;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (stack == null)
            stack = new Stack();
        // 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);
        }
    }
    

    FirstActivity.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);
        }
    }
    

    ThirdActivity.java

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

    Add Below XML files into your res/layout folder.

    1) tab_screen.xml

    
    
    
        
    
            
    
            
        
    
    
    

    2) main.xml

    
    
    
        
    
    
    

    AndroidManifest.xml:-

    
    
    
        
    
        
            
                
                    
    
                    
                
            
            
                
                    
    
                    
                
            
            
                
                    
    
                    
                
            
            
                
                    
    
                    
                
            
            
                
                    
    
                    
                
            
        
    
    
    

    And see below link for more information on add multiple activities under one TabActivity with complete example.

    Android - Multiple Android Activities under one TabActivity

提交回复
热议问题