android底部菜单应用/Android TabHost的标签放在底部,已写成框架,继承即可

Android TabHost的标签放在底部,已写成框架,继承即可

感受一下不错的述说:

【每日话题】Android是自学好还是培训好,你有啥高招?
http://www.eoeandroid.com/thread-193467-1-1.html

【每日话题】留在这城市的理由
http://www.eoeandroid.com/thread-191466-1-1.html

【每日话题】面试那些事儿—面霸是怎样炼成的
http://www.eoeandroid.com/thread-193078-1-1.html

 

网上已经有很多关于如何将TabHost的标签放在底部,这里就不说了

主要是把这些都做成框架,只需要提供图片和文字就可以实现这样的效果。

直接上图,代码解释的很清楚

程序的图片借用新浪微博.

 

使用代码:

复制代码
package com.api.example.app;
 
import java.util.ArrayList;
import java.util.List;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabWidget;
import android.widget.TextView;
 
import com.api.R;
import com.api.TabHostActivity;
 
/**
* 整个流程就像使用ListView自定BaseAdapter一样
 
* 
* 如果要自定义TabHostActivity的Theme,并且不想要头部阴影
* 一定要添加这个android:windowContentOverlay = null
 
* 
* 如果想在别的项目里面使用TabHostActivity
* 可以项目的属性里面找到Android,然后在Library部分添加这个项目(Api)
* <a href='\"http://www.cnblogs.com/qianxudetianxia/archive/2011/05/01/2030232.html\"' target='\"_blank\"'>如何添加</a>
 
* */
public class ExampleActivity extends TabHostActivity {
 
List mItems;
private LayoutInflater mLayoutInflater;
 
/**在初始化TabWidget前调用
* 和TabWidget有关的必须在这里初始化*/
@Override
protected void prepare() {
TabItem home = new TabItem(
"首页", // title
R.drawable.icon_home, // icon
R.drawable.example_tab_item_bg, // background
new Intent(this, Tab1Activity.class)); // intent
 
TabItem info = new TabItem(
"资料",
R.drawable.icon_selfinfo,
R.drawable.example_tab_item_bg,
new Intent(this, Tab2Activity.class));
 
TabItem msg = new TabItem(
"信息",
R.drawable.icon_meassage,
R.drawable.example_tab_item_bg,
new Intent(this, Tab3Activity.class));
 
TabItem square = new TabItem(
"广场",
R.drawable.icon_square,
R.drawable.example_tab_item_bg,
new Intent(this, Tab4Activity.class));
 
TabItem more = new TabItem(
"更多",
R.drawable.icon_more,
R.drawable.example_tab_item_bg,
new Intent(this, Tab5Activity.class));
 
mItems = new ArrayList();
mItems.add(home);
mItems.add(info);
mItems.add(msg);
mItems.add(square);
mItems.add(more);
 
// 设置分割线
TabWidget tabWidget = getTabWidget();
tabWidget.setDividerDrawable(R.drawable.tab_divider);
 
mLayoutInflater = getLayoutInflater();
}
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCurrentTab(0);
}
 
/**tab的title,icon,边距设定等等*/
@Override
protected void setTabItemTextView(TextView textView, int position) {
textView.setPadding(3, 3, 3, 3);
textView.setText(mItems.get(position).getTitle());
textView.setBackgroundResource(mItems.get(position).getBg());
textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems.get(position).getIcon(), 0, 0);
 
}
 
/**tab唯一的id*/
@Override
protected String getTabItemId(int position) {
return mItems.get(position).getTitle(); // 我们使用title来作为id,你也可以自定
}
 
/**点击tab时触发的事件*/
@Override
protected Intent getTabItemIntent(int position) {
return mItems.get(position).getIntent();
}
 
@Override
protected int getTabItemCount() {
return mItems.size();
}
 
/**自定义头部文件*/
@Override
protected View getTop() {
return mLayoutInflater.inflate(R.layout.example_top, null);
}
 
}
复制代码

框架代码:

复制代码
package com.api;
 
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
 
public abstract class TabHostActivity extends TabActivity {
 
private TabHost mTabHost;
private TabWidget mTabWidget;
private LayoutInflater mLayoutflater;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set theme because we do not want the shadow
setTheme(R.style.Theme_Tabhost);
setContentView(R.layout.api_tab_host);
 
mLayoutflater = getLayoutInflater();
 
mTabHost = getTabHost();
mTabWidget = getTabWidget();
//mTabWidget.setStripEnabled(false); // need android2.2
 
prepare();
 
initTop();
initTabSpec();
}
 
private void initTop() {
View child = getTop();
LinearLayout layout = (LinearLayout) findViewById(R.id.tab_top);
layout.addView(child);
}
 
private void initTabSpec() {
 
int count = getTabItemCount();
 
for (int i = 0; i < count; i++) {
// set text view
View tabItem = mLayoutflater.inflate(R.layout.api_tab_item, null);
 
TextView tvTabItem = (TextView) tabItem.findViewById(R.id.tab_item_tv);
setTabItemTextView(tvTabItem, i);
// set id
String tabItemId = getTabItemId(i);
// set tab spec
TabSpec tabSpec = mTabHost.newTabSpec(tabItemId);
tabSpec.setIndicator(tabItem);
tabSpec.setContent(getTabItemIntent(i));
 
mTabHost.addTab(tabSpec);
}
 
}
 
/** 在初始化界面之前调用 */
protected void prepare() {
// do nothing or you override it
}
 
/** 自定义头部布局 */
protected View getTop() {
// do nothing or you override it
return null;
}
 
protected int getTabCount() {
return mTabHost.getTabWidget().getTabCount();
}
 
/** 设置TabItem的图标和标题等*/
abstract protected void setTabItemTextView(TextView textView, int position);
 
abstract protected String getTabItemId(int position);
 
abstract protected Intent getTabItemIntent(int position);
 
abstract protected int getTabItemCount();
 
protected void setCurrentTab(int index) {
mTabHost.setCurrentTab(index);
}
 
protected void focusCurrentTab(int index) {
mTabWidget.focusCurrentTab(index);
}
 
}
复制代码


 源码下载

 





在android中实现菜单功能有多种方法。

http://www.iteye.com/topic/1116261

Options Menu:用户按下menu Button时显示的菜单。

Context Menu:用户长时间按下屏幕,所显示出来的菜单也称为上下文菜单。

Submenu:子菜单。

但是有时候这些内置的菜单并不能满足我们功能,这就需要自己自定义一种菜单。接下来我说的这种就是通过TabHost与RadioGroup结合完成的菜单。这也是很常用的一种底部菜单做法。先上图:

 



 

首先看布局文件:

 

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <LinearLayout   
  5.         android:orientation="vertical"   
  6.         android:layout_width="fill_parent"   
  7.         android:layout_height="fill_parent">  
  8.         <FrameLayout   
  9.             android:id="@android:id/tabcontent"   
  10.             android:layout_width="fill_parent"   
  11.             android:layout_height="0.0dip"   
  12.             android:layout_weight="1.0" />  
  13.         <TabWidget   
  14.             android:id="@android:id/tabs"   
  15.             android:visibility="gone"   
  16.             android:layout_width="fill_parent"   
  17.             android:layout_height="wrap_content"   
  18.             android:layout_weight="0.0" />  
  19.         <RadioGroup   
  20.             android:gravity="center_vertical"   
  21.             android:layout_gravity="bottom"   
  22.             android:orientation="horizontal"   
  23.             android:id="@+id/main_radio"   
  24.             android:background="@drawable/maintab_toolbar_bg"   
  25.             android:layout_width="fill_parent"   
  26.             android:layout_height="wrap_content">  
  27.             <RadioButton   
  28.                 android:id="@+id/radio_button0"   
  29.                 android:tag="radio_button0"   
  30.                 android:layout_marginTop="2.0dip"   
  31.                 android:text="@string/alarm"   
  32.                 android:drawableTop="@drawable/icon_1"   
  33.                 style="@style/main_tab_bottom" />  
  34.             <RadioButton   
  35.                 android:id="@+id/radio_button1"   
  36.                 android:tag="radio_button1"   
  37.                 android:layout_marginTop="2.0dip"   
  38.                 android:text="@string/message"   
  39.                 android:drawableTop="@drawable/icon_2"   
  40.                 style="@style/main_tab_bottom" />  
  41.             <RadioButton   
  42.                 android:id="@+id/radio_button2"   
  43.                 android:tag="radio_button2"   
  44.                 android:layout_marginTop="2.0dip"   
  45.                 android:text="@string/photo"   
  46.                 android:drawableTop="@drawable/icon_3"   
  47.                 style="@style/main_tab_bottom" />  
  48.             <RadioButton   
  49.                 android:id="@+id/radio_button3"   
  50.                 android:tag="radio_button3"   
  51.                 android:layout_marginTop="2.0dip"   
  52.                 android:text="@string/music"   
  53.                 android:drawableTop="@drawable/icon_4"   
  54.                 style="@style/main_tab_bottom" />  
  55.             <RadioButton   
  56.                 android:id="@+id/radio_button4"   
  57.                 android:tag="radio_button4"   
  58.                 android:layout_marginTop="2.0dip"   
  59.                 android:text="@string/setting"   
  60.                 android:drawableTop="@drawable/icon_5"   
  61.                 style="@style/main_tab_bottom" />  
  62.         </RadioGroup>  
  63.     </LinearLayout>  
  64. </TabHost>  

 

需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost   ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:

 

在TabActivity中有么个方法:

Java代码   收藏代码
  1. @Override  
  2.    public void onContentChanged() {  
  3.        super.onContentChanged();  
  4.        mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);  
  5.   
  6.        if (mTabHost == null) {  
  7.            throw new RuntimeException(  
  8.                    "Your content must have a TabHost whose id attribute is " +  
  9.                    "'android.R.id.tabhost'");  
  10.        }  
  11.        mTabHost.setup(getLocalActivityManager());  
  12.    }  
  13.   
  14.    private void ensureTabHost() {  
  15.        if (mTabHost == null) {  
  16.            this.setContentView(com.android.internal.R.layout.tab_content);  
  17.        }  
  18.    }  

 当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。

 

再看看TabHost这个类中,

 

Java代码   收藏代码
  1. public void setup() {  
  2.       mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);  
  3.       if (mTabWidget == null) {  
  4.           throw new RuntimeException(  
  5.                   "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");  
  6.       }  
  7.   
  8.       // KeyListener to attach to all tabs. Detects non-navigation keys  
  9.       // and relays them to the tab content.  
  10.       mTabKeyListener = new OnKeyListener() {  
  11.           public boolean onKey(View v, int keyCode, KeyEvent event) {  
  12.               switch (keyCode) {  
  13.                   case KeyEvent.KEYCODE_DPAD_CENTER:  
  14.                   case KeyEvent.KEYCODE_DPAD_LEFT:  
  15.                   case KeyEvent.KEYCODE_DPAD_RIGHT:  
  16.                   case KeyEvent.KEYCODE_DPAD_UP:  
  17.                   case KeyEvent.KEYCODE_DPAD_DOWN:  
  18.                   case KeyEvent.KEYCODE_ENTER:  
  19.                       return false;  
  20.   
  21.               }  
  22.               mTabContent.requestFocus(View.FOCUS_FORWARD);  
  23.               return mTabContent.dispatchKeyEvent(event);  
  24.           }  
  25.   
  26.       };  
  27.   
  28.       mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {  
  29.           public void onTabSelectionChanged(int tabIndex, boolean clicked) {  
  30.               setCurrentTab(tabIndex);  
  31.               if (clicked) {  
  32.                   mTabContent.requestFocus(View.FOCUS_FORWARD);  
  33.               }  
  34.           }  
  35.       });  
  36.   
  37.       mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);  
  38.       if (mTabContent == null) {  
  39.           throw new RuntimeException(  
  40.                   "Your TabHost must have a FrameLayout whose id attribute is "  
  41.                           + "'android.R.id.tabcontent'");  
  42.       }  
  43.   }  

 这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。

 

在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。

 

看一下主要代码:

Java代码   收藏代码
  1. package com.iteye.androidtoast;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.RadioGroup.OnCheckedChangeListener;  
  8. import android.widget.TabHost;  
  9.   
  10. public class MainActivity extends TabActivity implements OnCheckedChangeListener{  
  11.     /** Called when the activity is first created. */  
  12.     private TabHost mHost;  
  13.     private RadioGroup radioderGroup;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.maintabs);  
  18.         //实例化TabHost  
  19.         mHost=this.getTabHost();  
  20.           
  21.         //添加选项卡  
  22.         mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")  
  23.                     .setContent(new Intent(this,OneActivity.class)));  
  24.         mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")  
  25.                 .setContent(new Intent(this,TwoActivity.class)));  
  26.         mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")  
  27.                 .setContent(new Intent(this,ThreeActivity.class)));  
  28.         mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")  
  29.                 .setContent(new Intent(this,FourActivity.class)));  
  30.         mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")  
  31.                 .setContent(new Intent(this,FiveActivity.class)));  
  32.           
  33.         radioderGroup = (RadioGroup) findViewById(R.id.main_radio);  
  34.         radioderGroup.setOnCheckedChangeListener(this);  
  35.     }  
  36.     @Override  
  37.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  38.         switch(checkedId){  
  39.         case R.id.radio_button0:  
  40.             mHost.setCurrentTabByTag("ONE");  
  41.             break;  
  42.         case R.id.radio_button1:  
  43.             mHost.setCurrentTabByTag("TWO");  
  44.             break;  
  45.         case R.id.radio_button2:  
  46.             mHost.setCurrentTabByTag("THREE");  
  47.             break;  
  48.         case R.id.radio_button3:  
  49.             mHost.setCurrentTabByTag("FOUR");  
  50.             break;  
  51.         case R.id.radio_button4:  
  52.             mHost.setCurrentTabByTag("FIVE");  
  53.             break;  
  54.         }         
  55.     }  
  56. }  

 这个应该没有什么好难理解的。OK该睡了。有什么问题留言!

 




GridView + ViewFlipper布局界面,模仿“机锋市场”

http://www.iteye.com/topic/1116298

源码在附件中,效果如图片 
 

 

android Tab 位于底部 ,且每一个Tab选项对应一个Activity

http://blog.csdn.net/jamesliulyc/article/details/6727325

紧接上一篇巧妙运用ViewStub写出类似Tab选项卡(想怎么写tab就怎么写,横着写竖着写随你) ,上一篇耦合度实在是太高了(其实那个性能也不咋滴),饱受那一堆乱稻草捆绑在一起的痛苦,所以进行了一系列的改造。

第一步:首先你得了解TabHost和TabWidget, 将TabHost分为两个部分,一个是放内容的,还有一个就是放选项卡的,我们这里选项卡用TabWidget。TabHost的id应该写为

android:id="@android:id/tabhost",而不是以前的那个加号,这样可以直接在Activity里面通过getTabHost方法得到此TabHost对象。那如何才能将选项卡放在最底部呢,这其实很简单,还记得 RelativeLayout中android:layout_alignBottom 这个不,也就是说将TabWidget和选项卡内容放到RelativeLayout布局里,废话不多说了,看代码

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:id="@android:id/tabhost"  
  5.     xmlns:android="http://schemas.android.com/apk/res/android"  
  6.     >  
  7.     <RelativeLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:padding="3dp"  
  12.     >  
  13.          <FrameLayout  
  14.              android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:id="@android:id/tabcontent"  
  17.             android:layout_weight="1"   
  18.              >  
  19.          </FrameLayout>  
  20.           <TabWidget  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="50dip"  
  23.             android:id="@android:id/tabs"  
  24.             android:layout_alignBottom = "@android:id/tabcontent"  
  25.             />  
  26.     </RelativeLayout>  
  27. </TabHost>  


以上xml代码可以通用哈,FrameLayout里用来放内容的,Tab的xml基本结构就这样搭好了,

第二步:接下来就是往Activity里面写点东东了,这个Activity必须要继承下TabActivity,要不然就算你setContentView了上面那个xml也没法通过getTabHost()获得布局中的tabHost。

然后通过getTabHost得到TabHost对象。我这里打算建立四个选项卡,newTabSpec 这是为你的每一个选项卡上面打上一个Tag,也就是标记标记而已,特殊情况下你可以通过这个来查找到你的选项卡。setIndicator就是在你的选项卡上设置一些内容,至于什么内容,你懂的,要么是文字,要么是图片,要么就是文字加图片,当然有时候复杂的话还会写上布局文件。我这里就放上一个图片吧,然后这个图片加上一些特效,懒得在xml写什么布局了,直接在代码写了,返回的是一个View对象,废话有点多,直接看代码吧

  1.   private  class TabView extends LinearLayout {  
  2.     ImageView imageView ;  
  3. public TabView(Context c, int drawable, int drawableselec) {  
  4.     super(c);  
  5.     imageView = new ImageView(c);  
  6.     StateListDrawable listDrawable = new StateListDrawable();  
  7.     listDrawable.addState(SELECTED_STATE_SET, this.getResources()  
  8.             .getDrawable(drawableselec));  
  9.     listDrawable.addState(ENABLED_STATE_SET, this.getResources()  
  10.             .getDrawable(drawable));  
  11.     imageView.setImageDrawable(listDrawable);  
  12.     imageView.setBackgroundColor(Color.TRANSPARENT);  
  13.     setGravity(Gravity.CENTER);  
  14.     addView(imageView);  
  15. }  


上面的代码也就是自定义的一个View吧。

下面这个就是构建选项卡以及内容(选中的是第二个选项卡),我这里就把每个不同的选项卡内容分别放在不同的Activity里面,然后通过Tab来将这些Activity合并在一起,这样显得比较有层次感。

  1. @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  5.         setContentView(R.layout.main_tab);  
  6.         TabHost tabHost=getTabHost();  
  7.           
  8.         TabView view = null;  
  9.           
  10.         // 最近联系人  
  11.         view = new TabView(this, R.drawable.bg_tab_dial_normal, R.drawable.bg_tab_dial_normal);  
  12.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  13.               
  14.         TabSpec recentContactSpec=tabHost.newTabSpec("RecentContact");  
  15.         recentContactSpec.setIndicator(view);  
  16.         Intent recentContactIntent = new Intent(this, RecentContactActivity.class);  
  17.         recentContactSpec.setContent(recentContactIntent);  
  18.         // 联系人  
  19.         view = new TabView(this, R.drawable.bg_tab_contact_normal, R.drawable.bg_tab_contact_normal);  
  20.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  21.           
  22.         TabSpec contactBookSpec=tabHost.newTabSpec("ContactBook");  
  23.         contactBookSpec.setIndicator(view);  
  24.         Intent contactBookIntent = new Intent(this,ContactBookActivity.class);  
  25.         contactBookSpec.setContent(contactBookIntent);  
  26.           
  27.         // 短信  
  28.         view = new TabView(this, R.drawable.bg_tab_sms_normal, R.drawable.bg_tab_sms_normal);  
  29.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  30.           
  31.         TabSpec smsMessageSpec = tabHost.newTabSpec("SmsMessage");  
  32.         smsMessageSpec.setIndicator(view);  
  33.         Intent smsMessageIntent = new Intent(this, SmsMessageActivity.class);  
  34.         smsMessageSpec.setContent(smsMessageIntent);  
  35.           
  36.         //设置   
  37.         view = new TabView(this, R.drawable.bg_tab_setting_normal, R.drawable.bg_tab_setting_normal);  
  38.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  39.           
  40.         TabSpec settingSpec = tabHost.newTabSpec("Setting");  
  41.         settingSpec.setIndicator(view);  
  42.         Intent settingIntent = new Intent(this, SettingActivity.class);  
  43.         settingSpec.setContent(settingIntent);  
  44.           
  45.         tabHost.addTab(recentContactSpec);  
  46.         tabHost.addTab(contactBookSpec);  
  47.         tabHost.addTab(smsMessageSpec);  
  48.         tabHost.addTab(settingSpec);  
  49.           
  50.         tabHost.setCurrentTab(1);  
  51.     }  
  52.       


这个我没有写Demo,也就不提供详细的Demo了,不过还是截个图吧

 


Tab的多种典型用法总结(带动画)

http://www.iteye.com/topic/1118818

里面总结的是tab的一些比较典型的用法,带有多种动画效果,希望对各位有所帮助。
下面是相关截图:
1.jpg

2.jpg

3.jpg

4.jpg

5.jpg

6.jpg

 

 






  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为你提供一个简单的示例代码,演示如何在Android Studio中动态添加项目的TabHost。 1. 首先,在XML布局文件中添加TabHost组件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <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="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </TabHost> ``` 2. 在Java代码中找到对应的TabHost组件并初始化: ```java TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 创建一个新的TabSpec对象,用于描述每个标签页: ```java TabHost.TabSpec spec; spec = tabHost.newTabSpec("tag1"); spec.setIndicator("Tab1"); spec.setContent(R.id.tab1); tabHost.addTab(spec); ``` 4. 重复步骤3,以添加其他标签页: ```java spec = tabHost.newTabSpec("tag2"); spec.setIndicator("Tab2"); spec.setContent(R.id.tab2); tabHost.addTab(spec); spec = tabHost.newTabSpec("tag3"); spec.setIndicator("Tab3"); spec.setContent(R.id.tab3); tabHost.addTab(spec); ``` 5. 最后,你可以在代码中动态添加标签页: ```java TabHost.TabSpec spec = tabHost.newTabSpec("tag4"); spec.setIndicator("Tab4"); spec.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { TextView textView = new TextView(MainActivity.this); textView.setText("This is Tab 4"); return textView; } }); tabHost.addTab(spec); ``` 这个示例演示了如何在Android Studio中动态添加项目的TabHost。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值