Android:TabHost
2016-01-06 12:21
405 查看
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabhost"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/page1"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是第一个标签页"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/page2"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是第二个标签页"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/page3"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是第三个标签页"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
tabwidget中的一个条目
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
>
<TextView
android:background="@drawable/tab_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_marginRight="1dp"
android:id="@+id/name"
/>
</LinearLayout>
条目背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/bg_selected" /> <!-- pressed -->
<item android:state_selected="true" android:drawable="@drawable/bg_selected" />
<item android:drawable="@drawable/bg_normal" /> <!-- default -->
</selector>
public class MainActivity extends Activity {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Debug.startMethodTracing("itcast");
tabHost = (TabHost) this.findViewById(R.id.tabhost);
tabHost.setup();
TabSpec tabSpec = tabHost.newTabSpec("page1");
//tabSpec.setIndicator("首页", getResources().getDrawable(R.drawable.i1));
tabSpec.setIndicator(createTabView("首页"));
tabSpec.setContent(R.id.page1);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("page2");
// tabSpec.setIndicator("第二页", getResources().getDrawable(R.drawable.i2));
tabSpec.setIndicator(createTabView("第二页"));
tabSpec.setContent(R.id.page2);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("page3");
//tabSpec.setIndicator("第三页", getResources().getDrawable(R.drawable.i7));
tabSpec.setIndicator(createTabView("第三页"));
tabSpec.setContent(R.id.page3);
tabHost.addTab(tabSpec);
tabHost.setCurrentTab(0);
}
@Override
protected void onDestroy() {
Debug.stopMethodTracing();
super.onDestroy();
}
private View createTabView(String name) {
//View tabView = getLayoutInflater().inflate(R.layout.tab, null);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(0xFFFFFF);
TextView textView = new TextView(this);
textView.setText(name);
textView.setBackgroundResource(R.drawable.tab_bg);
textView.setTextColor(0xFFFFFF);
textView.setTextSize(18.0f);
textView.setGravity(Gravity.CENTER);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.addView(textView, params);
return linearLayout;
}
}
相关文章推荐
- 在Android上实现多进程构架的浏览器(浏览器开发)的讨论
- Android:ListView数据的分批加载
- Android系统之路(初识MTK) ------ 客制化开机LOGO跟开机动画/系统默认壁纸
- Android实现打电话和发短信
- Android:ListView数据异步加载、Handler、AsyncTask
- ArrayList 扩容 Android Java 真的不一样
- FindBugs-AndroidStudio代码错误自动检查插件
- 关于应用打开word、pdf
- Android导入外部数据库
- Android 视图框架系列2/3——SurfaceView视图框架
- 1.0 Android学习路线简要介绍
- Android SQLite服务--创建、增删改查
- 获取APK的签名MD5值
- 开发基于高德SDK的Android车载导航应用
- Android:PopWindow
- AndroidManifest.xml文件解析
- 第1部分 Android环境搭建入门
- Android应用程序的debug属性
- android 读取assets下文件或者 java读取本地文件
- AndroidStudio 成长之路之Theme Editor