Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。...TabHost是标签控件类的核心类,也是标签的集合。 1.tabhost定义 android控件中有封装好的tab控件,直接拖一个到xml文件中。下面的代码都是tab控件自己生成的。...11 TabHost 12 android:id="@android:id/tabhost" 13 android:layout_width="fill_parent...> 在后台,可以通过tabhost.addtab方法添加分页。...,tabhost.getTabContentView(),true); 3 tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator(
根标签及id 设置Android自带id : XML布局文件中, 可以使用TabHost> 标签设置, 其中的id 需要引用 android的自带id : android:id="@android:id...获取TabHost 获取方法 : getHost(); 前提 : 调用getHost()方法获取TabHost组件的方法的前提是在布局文件中, 设置了android自带的id android:id="@...android:id/tabhost" 才可以; 2....> TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost...; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public
无法显示TabHost 2. 添加图片 + 文字 无法同时 3. 说在最后:点击事件 4....底部导航无法实现 现在 从问题出发: 问题一:无法显示 TabHost 很多人调用TabHost的方法是: setContentView(R.layout.activity_main); tabHost...问题三:添加监听事件 这个无脑 只要 id 匹配就行了,直接上代码: tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){...附上布局与实现: 布局: 实现: public class MainActivity extends TabActivity { TabHost tabHost; @Override...(), true); //修改后 setContentView(R.layout.activity_main); tabHost = getTabHost(); tabHost.setup
二 TabHost使用方法 Android中的TabHost是一个容器类,用于实现选项卡布局。...以下是使用TabHost的一般步骤: 在XML布局文件中添加TabHost和相关的子控件: TabHost android:id="@android:id/tabhost" android...属性: android:id:在XML布局文件中使用的唯一标识符,用于引用TabHost对象。 android:tabWidgetId:指定TabWidget的ID,用于与TabHost关联。...android:tabContentId:指定内容视图的ID,用于与TabHost关联。 android:inflatedId:指定选项卡内容视图被填充后的ID。...四 TabHost简单案例 以下是一个简单的TabHost使用案例,用于实现两个选项卡:"Tab1"和"Tab2": import android.app.TabActivity; import android.os.Bundle
Android UI控件系列:TabWidget(切换卡) Tab选项卡类似与电话本的界面,通过多个标签切换不同的内容,要实现这个效果,首先要知道TabHost,它是一个用来存放多个Tab标签的容器,每一个...Tab都可以对应自己的布局,比如,电话本中的Tab布局就是一个线性布局 要使用TabHost,首先要通过getTabHost方法获取TabHost的对象,然后通过addTab方法来向TabHost中添加...; import android.app.TabActivity; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost...; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; public class TabTest...> TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost
: => android.view.View => android.view.ViewGroup => android.widget.FrameLayout => android.widget.TabHost...=> android.view.View => android.view.ViewGroup => android.widget.LinearLayout => android.widget.TabWidget...="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height...android:id="@+id/view2" android:background="@drawable/red" android:layout_width="fill_parent"...android:layout_height="fill_parent" android:text="@string/tabs_1_tab_2"/> android:id="@
1.布局 1 TabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http:...//schemas.android.com/tools" 3 android:id="@+id/tabHost" 4 android:layout_width="match_parent...ListActivity,TabHost必须设置为@android:id/tabhost 2、TabWidget必须设置android:id为@android:id/tabs 3、FrameLayout...需要设置android:id为@android:id/tabcontent 第二种:只是单纯的继承Activity类 布局文件 和上面一样 只是TabHost 的id 换为 tabHost = (TabHost...> 2 TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height
:id/tabhost" android:layout_width="match_parent" android:background="#3000" android...; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TabWidget; import...= (FragmentTabHost) findViewById(android.R.id.tabhost);//绑定tabhost tabHost.setup(this, getSupportFragmentManager...for (int i = 0; i < count; i++) { //设置每个TabHost布局 TabHost.TabSpec tabSpec = tabHost.newTabSpec...(tabSpec, fragmentArry[i], null); tabHost.setTag(i); } //初始化TabHost文字颜色
那么我们看下页面布局代码 TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id...="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">...:layout_height="1dip" android:layout_weight="1" /> TabHost>...如上面的代码所示: 整个窗体在一个TabHost元素下。...; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.TabSpec
FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 布局实现 实现tabhost采用android.support.v4.app.FragmentTabHost...注意 id:@android:id/tabhost 实现tabWidget 注意 id:@android:id/tabs 实现FrameLayout 注意 1. id: @android:...> android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android...初始化TabHost tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost); tabhost.setup(this, getSupportFragmentManager...(android.R.color.white); // 初始化 tab选中 tabhost.setCurrentTabByTag(TAB_CHAT); chatIndicator.setTabSelected
选项卡(TabHost)方便的在窗口上设置多个标签页,每个标签页相当于获得一个与外部容器相同大小的组件摆放区域 通过这种方式,可以在一个容器中放置多组件。...; 6 import android.content.Intent; 7 import android.content.res.Resources; 8 import android.os.Bundle...; 9 import android.widget.TabHost; 10 11 public class TobHost_lianxi extends TabActivity{ 12 @Override...); 18 Resources resources = getResources(); 19 TabHost tabHost = getTabHost(); 20...因为tabHost.setCurrentTab(1); 设置第2个添加的标签项为默认显示,从0开始算) ? ? ?
> TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id...TabHost 的 ID 应该为@android:id/tabhost。 TabWidget 的 ID 应该为@android:id/tabs。...; import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost; /** *...> TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mytabhost...; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TabHost
使用方式: 从TabActivity中用getTabHost()方法获取TabHost,然后设置标签内容 布局: TabHost必须设置android:id为@android:id/tabhost TabWidget...文件: TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:...id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android... TabHost> 代码: package com.example.user.tabhost; import android.support.v7...tab = (TabHost) findViewById(android.R.id.tabhost); //初始化TabHost容器 tab.setup();
当然具体实现又会有很多方式,我们本期就先来学习最简单的Tab切换吧,有一点类似于之前了解的TabHost。...> android="http://schemas.android.com/apk/res/android" android:orientation...; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup...二、自定义实现 上面我们使用了系统自带的控件来完成Tab显示,可能有的同学已经发现其与TabHost还是有一定的差别的,上面的Tab只显示3个,而且也不能完全满足实际需求,就需要我们自定义来实现了...> android="http://schemas.android.com/apk/res/android" android:layout_width
TabHost控件默认使用LinearLayout包裹TabWidget和FrameLayout,布局文件如下: TabHost xmlns:android="http://schemas.android.com.../apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android...方式一:将TabHost中默认的LinearLayout换成RelativeLayout,并给TabWidget添加Android:layout_alignParentBottom="true" TabHost.../apk/res/android">http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android...="bottom" TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost
|-----FrameLayout 布局实现 实现tabhost采用android.support.v4.app.FragmentTabHost 注意 id:@android:id/tabhost...将自定义展示的区域放到TabHost之上 3....> android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android...初始化TabHost tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost); tabhost.setup(this, getSupportFragmentManager...(android.R.color.white); // 初始化 tab选中 tabhost.setCurrentTabByTag(TAB_CHAT); chatIndicator.setTabSelected
把原生的TabWidget隐藏,用了个垂直的LinearLayout写, 下面是FrameLayout,也是TabHost必须写的 android="http..." > TabHost android:id="@android:id/tabhost" android:layout_width="match_parent"...public class MainActivity extends TabActivity implements OnClickListener { private TabHost tabHost;...conversation".equals(tabHost.getCurrentTabTag())){ tabHost.setCurrentTabByTag("conversation")...group".equals(tabHost.getCurrentTabTag())){ tabHost.setCurrentTabByTag("group"); slideBackGround.startAnimation
为ImageButton指定android:text属性没用,不会显示文字 可以指定android:background为按钮增加背景图片,但这图片是固定的 可以指定android:src为图片按钮增加图片属性...) TabHost仅仅只是一个简单的容器,它提供如下方法 newTabSpec(String tag) 创建选项卡 addTab(TabHost.TabSpec tabSpec) 添加选项卡 使用TabHost...另外需要注意的是TabHost、TabWidget、FrameLayout三个组件的android:id必须使用系统默认的名称,而不能自己随意定义,否则会出错。...实例: TabHost tabHost = getTabHost();或者findViewById也行,不过后面要有tabHost.setup(); //设置使用TabHost布局 LayoutInflater.from...(this).inflate(R.layout.main, tabHost.getTabContentView(), true); //添加第一个标签页 tabHost.addTab(tabHost.newTabSpec
R.id.content); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), MyFragment.class...="1" /> android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width...android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TabHost.TabSpec...; import android.widget.TextView; /** * @author vhreal * 功能描写叙述:自己定义TabHost */ public class MainTabActivity...对象,得到TabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup
> TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height=...="@drawable/icon_5_n" style="@style/main_tab_bottom" /> TabHost...class MainTabActivity extends TabActivity implements OnCheckedChangeListener { private TabHost...三、总结 在这之前如果要做这种效果我恐怕第一时间就会想到用ActivityGroup来做,主要是因为TabHost的TabWidget非常难看,用起 来也不方便。...其实从源码可以看出,TabActivity也是继承自ActivityGroup,这里结合了单选按钮和TabHost,各取其长,有时间 可以专门写一个这样的自定义控件:) 四、相关文章 [Android]
领取专属 10元无门槛券
手把手带您无忧上云