底部TabHost

自留备用

/**
 * <一句话功能简述>定制居底的TabHost<BR>
 * <功能详细描述>
 * 
 * @author chenli
 * @version [版本号, 2011-1-27]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MyTabHostFive extends TabActivity
{
    /**
     * TabHost控件
     */
    private TabHost mTabHost;

    /**
     * TabWidget控件
     */
    private TabWidget mTabWidget;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = this.getTabHost();
        /* 去除标签下方的白线 */
        mTabHost.setPadding(mTabHost.getPaddingLeft(),
                mTabHost.getPaddingTop(), mTabHost.getPaddingRight(),
                mTabHost.getPaddingBottom() - 5);
        Resources rs = getResources();

        Intent layout1intent = new Intent();
        layout1intent.setClass(this, Layout1.class);
        TabHost.TabSpec layout1spec = mTabHost.newTabSpec("layout1");
        layout1spec.setIndicator("layou1",
                rs.getDrawable(android.R.drawable.stat_sys_phone_call));
        layout1spec.setContent(layout1intent);
        mTabHost.addTab(layout1spec);

        Intent layout2intent = new Intent();
        layout2intent.setClass(this, Layout2.class);
        TabHost.TabSpec layout2spec = mTabHost.newTabSpec("layout2");
        layout2spec.setIndicator("layout2",
                rs.getDrawable(android.R.drawable.stat_sys_phone_call_forward));
        layout2spec.setContent(layout2intent);
        mTabHost.addTab(layout2spec);

        Intent layout3intent = new Intent();
        layout3intent.setClass(this, Layout3.class);
        TabHost.TabSpec layout3spec = mTabHost.newTabSpec("layout3");
        layout3spec.setIndicator("layout3",
                rs.getDrawable(android.R.drawable.stat_sys_phone_call_on_hold));
        layout3spec.setContent(layout3intent);
        mTabHost.addTab(layout3spec);

        /* 对Tab标签的定制 */
        mTabWidget = mTabHost.getTabWidget();
        for (int i = 0; i < mTabWidget.getChildCount(); i++)
        {
            /* 得到每个标签的视图 */
            View view = mTabWidget.getChildAt(i);
            /* 设置每个标签的背景 */
            if (mTabHost.getCurrentTab() == i)
            {
                view.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.number_bg_pressed));
            }
            else
            {
                view.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.number_bg));
            }
            /* 设置Tab间分割竖线的颜色 */
            // tabWidget.setBackgroundColor(Color.WHITE);
            /* 设置Tab间分割竖线的背景图片 */
            // tabWidget.setBackgroundResource(R.drawable.icon);
            /* 设置tab的高度 */
            mTabWidget.getChildAt(i).getLayoutParams().height = 50;
            TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
                    android.R.id.title);
            /* 设置tab内字体的颜色 */
            tv.setTextColor(Color.rgb(49, 116, 171));
        }

        /* 当点击Tab选项卡的时候,更改当前Tab标签的背景 */
        mTabHost.setOnTabChangedListener(new OnTabChangeListener()
        {
            @Override
            public void onTabChanged(String tabId)
            {
                for (int i = 0; i < mTabWidget.getChildCount(); i++)
                {
                    View view = mTabWidget.getChildAt(i);
                    if (mTabHost.getCurrentTab() == i)
                    {
                        view.setBackgroundDrawable(getResources().getDrawable(
                                R.drawable.number_bg_pressed));
                    }
                    else
                    {
                        view.setBackgroundDrawable(getResources().getDrawable(
                                R.drawable.number_bg));
                    }
                }
            }
        });
    }
}



layout

package com.android.tabhost;

public class Layout2 extends Activity
{
    private TextView textView = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        textView = (TextView) findViewById(R.id.text2);
        textView.setText("我是第二页!");
    }
}


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">
		<!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->
		<!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->
		<FrameLayout android:id="@android:id/tabcontent"
			android:layout_weight="1" android:layout_width="fill_parent"
			android:layout_height="fill_parent" />

		<TabWidget android:id="@android:id/tabs"
			android:layout_alignParentBottom="true" android:layout_width="fill_parent"
			android:layout_height="wrap_content" />
	</LinearLayout>
</TabHost>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
    <TextView
    	android:id="@+id/text1"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_gravity="center"
    	/>
</LinearLayout>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值