1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android动态添加顶部tab android选项卡(Tab)实现顶部和底部

android动态添加顶部tab android选项卡(Tab)实现顶部和底部

时间:2020-08-27 21:35:11

相关推荐

android动态添加顶部tab android选项卡(Tab)实现顶部和底部

1.自己创建一XML文件放于layout文件夹tab.xml(选项卡在顶部)

encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="/apk/res/android"

>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="5dp" />

如果需要选项卡置底部的话交换Frame和TabWidget 上下位置,再加属性android:layout_weight

赋值大于0

encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="/apk/res/android"

>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="4"

android:padding="5dp" />

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

2.注意继承TabActivity这里没有给出切换的几个Activity,自己创建几个试试

import android.app.TabActivity;

import android.content.Intent;

import android.content.res.Resources;

import android.os.Bundle;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

public class MyTabActivity extends TabActivity {

@Override

public void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.tab);//Tab页面的布局

Resources res =

getResources();

TabHost

tabHost = getTabHost(); // The activity

TabHost

// TabHost tabHost =

(TabHost)findViewById(android.R.id.tabhost);

TabSpec

spec;

Intent

intent; // Reusable Intent for each tab

//第一个TAB

intent =

new

Intent(this,ShowActivity.class);//新建一个Intent用作Tab1显示的内容(这里ShowActivity自定义)

spec =

tabHost.newTabSpec("tab1")//新建一个 Tab

.setIndicator("Tab1",

res.getDrawable(android.R.drawable.ic_media_play))//设置名称以及图标

.setContent(intent);//设置显示的intentx

tabHost.addTab(spec);//添加进去

//第二个TAB

intent =

new Intent(this,TopActivity.class);//第二个Intent用作Tab1显示的内容

spec =

tabHost.newTabSpec("tab2")//新建一个 Tab

.setIndicator("Tab2",

res.getDrawable(android.R.drawable.ic_menu_camera))//设置名称以及图标

.setContent(intent);

tabHost.addTab(spec);

spec =

tabHost.newTabSpec("").setContent(intent).setIndicator("第三个",res.getDrawable(R.drawable.icon));

tabHost.addTab(spec);

tabHost.setCurrentTab(0);//设置默认选中项

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。