Android实践之TabActivity
16lz
2022-04-17
一、效果
2、主要代码
(1).activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
(2)styles.xml
40dp
(3)strings.xml
ToolBar 首页 分类 购物车
(4)MainActivity.java
package com.example.toolbar;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.TabHost;public class MainActivity extends TabActivity implements View.OnClickListener{ private static final String TAG = "MainActivity"; private Bundle mBundle = new Bundle(); private TabHost tab_host; private LinearLayout ll_first, ll_second, ll_third; private String FIRST_TAG = "first"; private String SECOND_TAG = "second"; private String THIRD_TAG = "third"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBundle.putString("tag", TAG); ll_first = findViewById(R.id.ll_first); ll_second = findViewById(R.id.ll_second); ll_third = findViewById(R.id.ll_third); ll_first.setOnClickListener(this); ll_second.setOnClickListener(this); ll_third.setOnClickListener(this); tab_host = getTabHost(); tab_host.addTab(getNewTab(FIRST_TAG, R.string.menu_first, R.drawable.ic_launcher_background, Main2Activity.class)); tab_host.addTab(getNewTab(SECOND_TAG, R.string.menu_second, R.drawable.ic_launcher_background, Main3Activity.class)); tab_host.addTab(getNewTab(THIRD_TAG, R.string.menu_third, R.drawable.ic_launcher_background, Main4Activity.class)); changeContainerView(ll_first); } private TabHost.TabSpec getNewTab(String spec, int label, int icon, Class<?>cls) { Intent intent = new Intent(this, cls).putExtras(mBundle); return tab_host.newTabSpec(spec).setContent(intent).setIndicator(getString(label), getResources().getDrawable(icon)); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.ll_first: case R.id.ll_second: case R.id.ll_third: changeContainerView(view); } } private void changeContainerView(View v) { ll_first.setSelected(false); ll_second.setSelected(false); ll_third.setSelected(false); v.setSelected(true); if (v == ll_first) { tab_host.setCurrentTabByTag(FIRST_TAG); } else if (v == ll_second) { tab_host.setCurrentTabByTag(SECOND_TAG); } else if (v == ll_third) { tab_host.setCurrentTabByTag(THIRD_TAG); } }}
3、资源链接
如果还不清楚,https://download.csdn.net/download/chicaidecaiji/12409083给上完整实例
更多相关文章
- android 按钮置灰效果
- Android四大组件Service之bindService启动
- 三步实现Android悬浮效果
- Android中实现整个视图切换的左右滑动效果
- Android的Task和Activity相关
- Android第三十一期 - 市面上所有引导页的效果
- Android(安卓)Textview控件
- Android的GridView控件点击图片变暗效果
- Android(安卓)动画效果 --Animation 动画(讲解了所有的Android动