TabHost的两种实现方式

第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.

xml布局:
Java代码 复制代码  收藏代码
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent" android:layout_height="fill_parent">   
  3.     <LinearLayout android:id="@+id/widget_layout_Blue"  
  4.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  5.         android:orientation="vertical" >   
  6.         <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  7.             android:layout_height="wrap_content" android:text="EditText"  
  8.             android:textSize="18sp">   
  9.         </EditText>   
  10.         <Button android:id="@+id/widget30" android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content" android:text="Button">   
  12.         </Button>   
  13.     </LinearLayout>   
  14.     <LinearLayout android:id="@+id/widget_layout_red"  
  15.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  16.         android:orientation="vertical"  >   
  17.         <AnalogClock android:id="@+id/widget36"  
  18.             android:layout_width="wrap_content" android:layout_height="wrap_content">   
  19.         </AnalogClock>   
  20.     </LinearLayout>   
  21.     <LinearLayout android:id="@+id/widget_layout_green"  
  22.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.         android:orientation="vertical">   
  24.         <RadioGroup android:id="@+id/widget43"  
  25.             android:layout_width="166px" android:layout_height="98px"  
  26.             android:orientation="vertical">   
  27.             <RadioButton android:id="@+id/widget44"  
  28.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  29.                 android:text="RadioButton">   
  30.             </RadioButton>   
  31.             <RadioButton android:id="@+id/widget45"  
  32.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  33.                 android:text="RadioButton">   
  34.             </RadioButton>   
  35.         </RadioGroup>   
  36.     </LinearLayout>   
  37. </FrameLayout>   
  38.   
  39. java代码:   
  40. super.onCreate(savedInstanceState);   
  41.         myTabhost=this.getTabHost();   
  42.         //get Tabhost   
  43.         LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);   
  44.         myTabhost.setBackgroundColor(Color.argb(1502270150));   
  45.            
  46.         myTabhost   
  47.                 .addTab(myTabhost.newTabSpec("One")// make a new Tab   
  48.                         .setIndicator("A")   
  49.                         // set the Title and Icon   
  50.                         .setContent(R.id.widget_layout_Blue));   
  51.         // set the layout   
  52.   
  53.         myTabhost   
  54.                 .addTab(myTabhost.newTabSpec("Two")// make a new Tab   
  55.                         .setIndicator("B",   
  56.                                 getResources().getDrawable(R.drawable.mumule))   
  57.                         // set the Title and Icon   
  58.                         .setContent(R.id.widget_layout_green));   
  59.         // set the layout   
  60.   
  61.         myTabhost   
  62.                 .addTab(myTabhost.newTabSpec("Three")// make a new Tab   
  63.                         .setIndicator("C",   
  64.                                 getResources().getDrawable(R.drawable.notepad))   
  65.                         // set the Title and Icon   
  66.                         .setContent(R.id.widget_layout_red)); 

第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.

xml布局:

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3.     android:id="@+id/hometabs"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"     
  6.     android:layout_height="fill_parent">    
  7.     <TabHost android:id="@+id/tabhost"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content">   
  10.         <LinearLayout   
  11.             android:orientation="vertical"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="fill_parent">   
  14.                
  15.             <TabWidget android:id="@android:id/tabs"    
  16.               android:orientation="horizontal"  
  17.               android:layout_width="wrap_content"  
  18.               android:layout_height="wrap_content">   
  19.             </TabWidget>   
  20.             
  21.              <FrameLayout android:id="@android:id/tabcontent"  
  22.                   android:layout_width="wrap_content"  
  23.                   android:layout_height="wrap_content">   
  24.                     <TextView android:id="@+id/view1"  
  25.                         android:layout_width="fill_parent"  
  26.                         android:layout_height="fill_parent"/>   
  27.                     <TextView android:id="@+id/view2"  
  28.                         android:layout_width="fill_parent"  
  29.                         android:layout_height="fill_parent"/>   
  30.                     <TextView android:id="@+id/view3"  
  31.                         android:layout_width="fill_parent"  
  32.                         android:layout_height="fill_parent"/>   
  33.              </FrameLayout>   
  34.             
  35.          </LinearLayout>   
  36.     </TabHost>   
  37. </LinearLayout>   
  38.   
  39. java代码:   
  40. protected void onCreate(Bundle savedInstanceState) {   
  41.         super.onCreate(savedInstanceState);   
  42.         setContentView(R.layout.hometabs);   
  43.            
  44.         TabHost tabHost = (TabHost) findViewById(R.id.tabhost);   
  45.         tabHost.setup();   
  46.         TabWidget tabWidget = tabHost.getTabWidget();   
  47.            
  48.         tabHost.addTab(tabHost.newTabSpec("tab1")   
  49.                 .setIndicator("tab1", getResources().getDrawable(R.drawable.mumule))   
  50.                 .setContent(R.id.view1));   
  51.            
  52.         tabHost.addTab(tabHost.newTabSpec("tab3")   
  53.                 .setIndicator("tab3")   
  54.                 .setContent(R.id.view3));   
  55.            
  56.         tabHost.addTab(tabHost.newTabSpec("tab2")   
  57.                 .setIndicator("tab2")   
  58.                 .setContent(R.id.view2));   
  59.            
  60.         final int tabs = tabWidget.getChildCount();   
  61.         Log.i(TAG, "***tabWidget.getChildCount() : " + tabs);   
  62.            
  63.         final int tabWidth = 90;   
  64.         final int tabHeight = 45;   
  65.            
  66.         for (int i = 0; i < tabs; i++) {   
  67.         /*  final View view = tabWidget.getChildAt(i);  
  68.             view.getLayoutParams().width = tabWidth;  
  69.             view.getLayoutParams().height = tabHeight;  
  70.             final TextView tv = (TextView) view.findViewById(android.R.id.title);  
  71.             tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));  
  72.             MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams();  
  73.             tvMLP.bottomMargin = 8;*/  
  74.         }   
  75.     }  
  • Tab.rar (130.9 KB)
  • 下载次数: 504
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHost 是 Android 中常用的一个布局控件,可以用于实现选项卡的效果。要实现左侧选项卡,可以通过以下步骤: 1. 在布局文件中,使用 TabHost 控件,并设置其高度和宽度为 match_parent。 2. 在 TabHost 中添加一个 TabWidget 控件,用于显示选项卡标签。 3. 在 TabHost 中添加一个 FrameLayout 控件,用于显示选项卡内容。 4. 在代码中,使用 TabHost.newTabSpec() 方法创建一个新的选项卡,设置其标签和内容,并将其添加到 TabHost 中。 5. 在 TabWidget 中设置选项卡标签的样式,例如设置背景颜色、文字颜色等等。 6. 在 TabHost 中设置选项卡的切换方式,例如设置为点击切换或滑动切换。 以下是一个简单的示例代码,演示如何实现左侧选项卡: ``` <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#f0f0f0" android:orientation="vertical" android:layout_alignLeft="@android:id/tabcontent" android:layout_alignStart="@android:id/tabcontent"> </TabWidget> ``` Java 代码: ``` TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); // 创建一个新的选项卡 TabHost.TabSpec spec1 = tabHost.newTabSpec("tab1"); spec1.setIndicator("选项卡1"); spec1.setContent(R.id.tab1); tabHost.addTab(spec1); // 创建另一个选项卡 TabHost.TabSpec spec2 = tabHost.newTabSpec("tab2"); spec2.setIndicator("选项卡2"); spec2.setContent(R.id.tab2); tabHost.addTab(spec2); // 设置选项卡的切换方式 tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // 处理选项卡切换事件 } }); // 设置选项卡标签的样式 TabWidget tabWidget = tabHost.getTabWidget(); for (int i = 0; i < tabWidget.getChildCount(); i++) { View view = tabWidget.getChildAt(i); view.setBackgroundColor(Color.parseColor("#ffffff")); TextView textView = view.findViewById(android.R.id.title); textView.setTextColor(Color.parseColor("#000000")); } ``` 以上代码中,通过添加两个选项卡实现左侧选项卡的效果。您可以根据需要添加更多的选项卡,并自定义选项卡标签的样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值