您的位置:首页 > 移动开发 > Android开发

Android TableHost 控件

2016-05-20 13:46 323 查看
        刚开始实习,第一次接触到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




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: