TabHost便签的用法
2016-05-05 20:56
239 查看
TabHost可以产生分栏的效果。具体实现过程:
首先,在布局文件中实现,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StartWatch" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StopWatch" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bAddTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add a Tab" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
然后还需要在MainActivity里面写代码才能完全把tabhost的功能实现出来,代码如下:
package com.example.tabhost;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
a1a7
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=(TabHost)findViewById(R.id.tabhost);
th.setup();
TabSpec specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab1);
specs.setIndicator("StopWatch");//设置选项卡的标题
th.addTab(specs);
specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab2);
specs.setIndicator("tab 2");//设置选项卡的标题
th.addTab(specs);
specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab3);
specs.setIndicator("Add a tab");//设置选项卡的标题
th.addTab(specs);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
首先,在布局文件中实现,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StartWatch" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StopWatch" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bAddTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add a Tab" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
然后还需要在MainActivity里面写代码才能完全把tabhost的功能实现出来,代码如下:
package com.example.tabhost;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
a1a7
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=(TabHost)findViewById(R.id.tabhost);
th.setup();
TabSpec specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab1);
specs.setIndicator("StopWatch");//设置选项卡的标题
th.addTab(specs);
specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab2);
specs.setIndicator("tab 2");//设置选项卡的标题
th.addTab(specs);
specs=th.newTabSpec("tab1");
specs.setContent(R.id.tab3);
specs.setIndicator("Add a tab");//设置选项卡的标题
th.addTab(specs);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
相关文章推荐
- spring从头开始(一)
- 解决android中listview中嵌套checkbox滑动时checkbox状态改变问题
- 排序算法java 二 --基数排序、归并排序
- linux下指令集
- The type java.lang.Object cannot be resolved It is indirectly referenced ...
- Shell中的while循环【转】
- H5 适配 动画animation js touch
- Leetcode - Length of Last Word
- 获取列表中多个相同元素的索引值
- 第十四章 老板回来了,我不知道—观察者模式
- Git与Repo入门
- 大数据学习笔记2-MapReduce
- hive笔记-----查询数据
- android studio 包依赖
- oracle 参考
- Oracle学习系列2
- HTML 文本框 TextBox 内容改变事件
- 【软件测试】JUnit、Hamcrest、Eclemma的安装
- Java集合框架
- 时间类扩展