TabHost简单的代码实现

来源:互联网 发布:淘宝的读书软件 编辑:程序博客网 时间:2024/05/16 08:59

下面给大家带来一个TabHost的简单实现

in.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@android:id/tabhost"     >    <LinearLayout     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >        <TabWidget         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@android:id/tabs"        ></TabWidget>        <FrameLayout         android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:id="@android:id/tabcontent"        >        <LinearLayout             android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/widget_layout_red"            android:background="#ff0000"            android:orientation="vertical"            ></LinearLayout>                 <LinearLayout             android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/widget_layout_yellow"            android:background="#FCD209"            android:orientation="vertical"            ></LinearLayout>            </FrameLayout></LinearLayout></TabHost>

继承TabActivity (方法一)

public class MainActivity extends TabActivity {private TabHost tabhost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.demo);                //从TabActivity上面获取放置Tab的TabHost        tabhost = getTabHost();        tabhost.addTab(tabhost                 //创建新标签one                .newTabSpec("one")                //设置标签标题                .setIndicator("红色")                //设置该标签的布局内容                .setContent(R.id.widget_layout_red));        tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow));    }}

方法二:继承Activity类
public class MainActivity extends Activity{private TabHost tabhost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.demo);                //得到TabHost对象实例        tabhost =(TabHost) findViewById(R.id.mytab);        //调用 TabHost.setup()        tabhost.setup();        //创建Tab标签        tabhost.addTab(tabhost.newTabSpec("one").setIndicator("红色").setContent(R.id.widget_layout_red));        tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow));        }}

以上代码就是本篇文章所描述代码.

希望能帮助到需要帮助的人.


原创粉丝点击