Android TableHost 控件

        刚开始实习,第一次接触到TabHost控件,在这里记录一下吧!

       layout文件:

<RelativeLayout 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"
    tools:context="com.example.tablehostdemo.MainActivity">
    <TabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabHost">
        <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="50dp"
                android:background="@android:color/white"
                android:tabStripEnabled="false"
                android:divider="@android:color/transparent">
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="12dp">

            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
源代码:


public class MainActivity extends Activity {
    TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost= (TabHost) findViewById(R.id.tabHost);

        getTabTitles();
    }
    private void getTabTitles(){
        tabHost.setup();
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        for (int i=0; i<3; i++) {
            View tab = inflater.inflate(R.layout.topic_tab_spec, null);
            TextView txtTitle = (TextView) tab.findViewById(R.id.txtTitle);
            txtTitle.setText("Title" +i);

            TextView textView = new TextView(MainActivity.this);
            textView.setId(i+3);

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
            tabHost.getTabContentView().addView(textView, params);


            tabHost.addTab(tabHost.newTabSpec("Tab" + i).setIndicator(tab).setContent(textView.getId()));
            System.out.println("textView id:" + textView.getId());
            getTopicList(textView,i);
        }

    }

    private void getTopicList(TextView textView,int code){
            textView.setText("content"+code);

    }
}


demo:http://download.csdn.net/detail/qq_31303013/9526256

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值