我的Android心得(7)--TabActivity
16lz
2021-12-04
要得到这样的界面:一个大的框架下有三个tab,tab之间可以切换
一、TabWidget
这是我的实现
activity_main.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" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout></TabHost>
注意tabhost 和 tabs 都是系统的id,不可以重新命名
android:id="@android:id/tabhost"<pre name="code" class="html">android:id="@android:id/tabs"
MainActivity.java :
public class MainActivity extends TabActivity{private TabHost tabhost;public static Target target;public void onCreate(Bundle saveInstanceState){super.onCreate(saveInstanceState);setContentView(R.layout.activity_main);tabhost = getTabHost();tabhost.addTab(tabhost.newTabSpec("tab_1").setIndicator("提醒", getResources().getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, RemindActivity.class)));tabhost.addTab(tabhost.newTabSpec("tab_2").setIndicator("设置", getResources().getDrawable(R.drawable.setting_tab_icon)).setContent(new Intent(this, SettingActivity.class)));tabhost.addTab(tabhost.newTabSpec("tab_3").setIndicator("关于", getResources().getDrawable(R.drawable.about_tab_icon)).setContent(new Intent(this, AboutActivity.class)));// 设置默认在第一个tabtabhost.setCurrentTab(0);/*tabhost.setOnTabChangedListener(new OnTabChangeListener(){@Overridepublic void onTabChanged(String tabId) {// TODO Auto-generated method stubif(tabId.equals("tab_1")){Toast.makeText(MainActivity.this, "in tab 1", Toast.LENGTH_SHORT).show();}else if(tabId.equals("tab_2")){Toast.makeText(MainActivity.this, "in tab 2", Toast.LENGTH_SHORT).show();}else if(tabId.equals("tab_3")){Toast.makeText(MainActivity.this, "in tab 3", Toast.LENGTH_SHORT).show();}}});*/}}
要实现选中某个tab,tab的图片会变化,要加入个selector.xml
http://www.apkbus.com/forum.php?mod=viewthread&tid=140042
二、ActivityGroup+Button
网上也有人是这样实现的,类似上面三、ViewPager
高大上,现在的微信就是ViewPager http://blog.csdn.net/wangjinyu501/article/details/8144332更多相关文章
- TextView 加链接所有方法
- Android(安卓)布局 & 一些控件
- android EditText 全面阐述
- 实现底部导航栏中间凸起
- Android(安卓)TextView 文字居中 .
- Android(安卓)Fresco属性大全,中文说明
- EditView某些属性说明
- Android中TextVIew一些属性
- Android(安卓)TextView 文字居中