ApiDemos之Tab
来源:互联网 发布:网络一线牵广场舞16步 编辑:程序博客网 时间:2024/05/18 01:54
1、通过findViewById
tabs1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/view1" android:background="@drawable/blue" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tabs_1_tab_1"/> <TextView android:id="@+id/view2" android:background="@drawable/red" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tabs_1_tab_2"/> <TextView android:id="@+id/view3" android:background="@drawable/green" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/tabs_1_tab_3"/></FrameLayout>
java文件:
TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab2") .setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(R.id.view3));效果:
2、Content By Factory
public class Tabs2 extends TabActivity implements TabHost.TabContentFactory { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on)) .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2") .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(this)); } /** {@inheritDoc} */ public View createTabContent(String tag) { final TextView tv = new TextView(this); tv.setText("Content for tab with tag " + tag); return tv; }}
效果:
3、Content By Intent
List8.java
public class List8 extends Activity{}
public class Tabs3 extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("list") .setContent(new Intent(this, List1.class))); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("photo list") .setContent(new Intent(this, List8.class))); // This tab sets the intent flag so that it is recreated each time // the tab is clicked. tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("destroy") .setContent(new Intent(this, Controls1.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); }}
效果:
4、增加holo主题
<activity android:name=".view.Tabs4" android:label="Views/Tabs/4. Non Holo theme" android:theme="@android:style/Theme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.SAMPLE_CODE"/> </intent-filter> </activity>
效果:
5、可滑动Tab页
tabs_scroll.xml
<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"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> </HorizontalScrollView> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" /> </LinearLayout></TabHost>
Tabs5.java
public class Tabs5 extends TabActivity implements TabHost.TabContentFactory { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabs_scroll); final TabHost tabHost = getTabHost(); for (int i=1; i <= 30; i++) { String name = "Tab " + i; tabHost.addTab(tabHost.newTabSpec(name) .setIndicator(name) .setContent(this)); } } /** {@inheritDoc} */ public View createTabContent(String tag) { final TextView tv = new TextView(this); tv.setText("Content for tab with tag " + tag); return tv; }}
效果:
0 0
- ApiDemos之Tab
- ApiDemos--将actionbar变成tab样式
- ApiDemos之Animation
- ApiDemos之Custom
- Apidemos之动画
- ApiDemos之ActionBarTab
- ApiDemos之AlarmManager
- Android ApiDemos之FragmentArguments
- #ApiDemos#view之Animation
- Android学习四之APIDemos
- ApiDemos之Fragment使用案例
- ApiDemos之activity recreate方法
- Android Animation学习之 ApiDemos
- Android Dialog学习之 ApiDemos
- Android Intent学习之 ApiDemos
- ApiDemos之activity切换动画
- ApiDemos知识点之Text(2)
- ApiDemos学习知识点之Sensors
- 认识电容
- XCode Debugger中的Icon符号的意义
- 你能不能像打游戏一样干工作
- 刘强东瘦了,京东在膨胀
- 经典数据结构之:哈希(链接法)
- ApiDemos之Tab
- FB和Twitter股价双双创新高 受益市场看涨社交广告
- 苹果受牵手中移动刺激股价涨近4%
- Android-- FragmentStatePagerAdapter分页
- Struts 2中的constant详解【转载】
- 当手机连接到电脑视频导不出来的时候
- 数据抓取之(一):抓取北京交通管理网站的车辆违法信息(已完结)
- TinyOS在Ubuntu10.04上的一键安装
- 黑马程序员_java面向对象总结【2】(静态、单例设计模式)