android中tabhost的使用
16lz
2022-05-31
布局总共有两种方法,第一种就是xml布局,还有就是自定义布局
Activity是 package cc.tabhost;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.TabHost;import android.widget.TabHost.TabSpec;import android.widget.TextView;public class MainActivity extends Activity {private TabHost tabhost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabhost = (TabHost) this.findViewById(R.id.tabhost);tabhost.setup();// 首先找到tabwidget与TabContent两个控件TabSpec tabSpec = tabhost.newTabSpec("page1");// 设置标记。以后可以通过该标记找到该页tabSpec.setIndicator("首页", getResources().getDrawable(R.drawable.i1));// 设置显示的字,与图片// tabSpec.setIndicator(createTabview("首页"));//自定义布局用的tabSpec.setIndicator(View// v)tabSpec.setContent(R.id.page1);tabhost.addTab(tabSpec);tabSpec = tabhost.newTabSpec("page2");tabSpec.setIndicator("第二页", getResources().getDrawable(R.drawable.i2));// tabSpec.setIndicator(createTabview("第二页"));tabSpec.setContent(R.id.page2);tabhost.addTab(tabSpec);tabSpec = tabhost.newTabSpec("page3");tabSpec.setIndicator("第三页", getResources().getDrawable(R.drawable.i3));// tabSpec.setIndicator(createTabview("第三页"));//自定义tabcontent布局tabSpec.setContent(R.id.page3);tabhost.addTab(tabSpec);tabhost.setCurrentTab(0);// 选择停留到第一个页为首页}/** * 自定义布局, * * @param 显示的首页 * ,第二页。。 * @return 一个view */private View createTabview(String string) {View v = getLayoutInflater().inflate(R.layout.tab, null);// 找到自定义的tab布局TextView tv = (TextView) (v.findViewById(R.id.name));tv.setText(string);return v;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
以上有自定义布局与xml两种方法的代码,以下是自定义布句的tab.xml <?xml version="1.0" encoding="utf-8"?>
更多相关文章
- Android(安卓)快速使用 string.xml 中的array标签
- Android(安卓)图片左上角、右上角标签tag
- Android的setTag
- Android(安卓)如何删除短信
- xml解析(一) XmlPullParser的使用
- TabHost自定义标签页(一)
- 【MarsChen】D03_Say_Hello_to_Android
- Android之selector标签
- Android(安卓)Studio学习之对文件的基本了解