main.xml布局文件:
Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Galleryandroid:id="@+id/gallery"android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:layout_marginTop="30dp"
  7. android:unselectedAlpha="1"android:spacing="1dip"/>
  8. </LinearLayout>


values/attrs.xml:
Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="Gallery">
  4. <attrname="android:galleryItemBackground"/>
  5. </declare-styleable>
  6. </resources>


values/strings.xml:
Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,Date!</string>
  4. <stringname="app_name">丸子-Widget</string>
  5. </resources>


drawable/tab_button_select.xml:
Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android">
  3. <gradientandroid:startColor="#FF1B1B1B"android:endColor="#FF969696"
  4. android:angle="90.0">
  5. </gradient>
  6. </shape>


drawable/tab_button_unselect.xml:
Java代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android">
  3. <gradientandroid:startColor="#FF000000"android:endColor="#FF474747"
  4. android:angle="90.0">
  5. </gradient>
  6. </shape>


IaiaiActivity.java类:
Java代码 收藏代码
  1. packagecom.iaiai.activity;
  2. importjava.util.Arrays;
  3. importjava.util.Collections;
  4. importjava.util.List;
  5. importandroid.app.Activity;
  6. importandroid.content.Context;
  7. importandroid.content.res.TypedArray;
  8. importandroid.graphics.Color;
  9. importandroid.os.Bundle;
  10. importandroid.view.Gravity;
  11. importandroid.view.View;
  12. importandroid.view.ViewGroup;
  13. importandroid.widget.AdapterView;
  14. importandroid.widget.AdapterView.OnItemClickListener;
  15. importandroid.widget.BaseAdapter;
  16. importandroid.widget.Gallery;
  17. importandroid.widget.TextView;
  18. /**
  19. *
  20. *<p>
  21. *Title:IaiaiActivity.java
  22. *</p>
  23. *<p>
  24. *E-Mail:[email protected]
  25. *</p>
  26. *<p>
  27. *QQ:176291935
  28. *</p>
  29. *<p>
  30. *Http:iaiai.iteye.com
  31. *</p>
  32. *<p>
  33. *Createtime:2011-6-26
  34. *</p>
  35. *
  36. *@author丸子
  37. *@version0.0.1
  38. */
  39. publicclassIaiaiActivityextendsActivity{
  40. privateGallerygallery;
  41. privateTabAdaptertextAdapter;
  42. privatestaticfinalString[]PROGRAM_NAMES={"中央一台","中央二台","中央三台",
  43. "中央四台","中央五台","中央六台","中央七台","中央八台",};
  44. @Override
  45. publicvoidonCreate(BundlesavedInstanceState){
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.main);
  48. gallery=(Gallery)findViewById(R.id.gallery);
  49. textAdapter=newTabAdapter(this,Arrays.asList(PROGRAM_NAMES));
  50. gallery.setAdapter(textAdapter);
  51. gallery.setOnItemClickListener(newOnItemClickListener(){
  52. @Override
  53. publicvoidonItemClick(AdapterView<?>parent,Viewview,
  54. intposition,longid){
  55. TabAdapteradapter=(TabAdapter)parent.getAdapter();
  56. adapter.setSelectedPos(position);
  57. }
  58. });
  59. }
  60. publicclassTabAdapterextendsBaseAdapter{
  61. privateContextmContext;
  62. privateList<String>mList;
  63. privateintmSelectedPos;
  64. publicTabAdapter(Contextcontext,List<String>list){
  65. mContext=context;
  66. TypedArraya=obtainStyledAttributes(R.styleable.Gallery);
  67. a.recycle();
  68. if(list==null)
  69. list=Collections.emptyList();
  70. mList=list;
  71. }
  72. publicvoidsetSelectedPos(intpos){
  73. if(pos!=mSelectedPos){
  74. mSelectedPos=pos;
  75. notifyDataSetChanged();
  76. }
  77. }
  78. publicintgetSelectedPos(){
  79. returnmSelectedPos;
  80. }
  81. publicintgetCount(){
  82. returnmList.size();
  83. }
  84. publicObjectgetItem(intposition){
  85. returnmList.get(position);
  86. }
  87. publiclonggetItemId(intposition){
  88. returnposition;
  89. }
  90. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  91. TextViewtext=null;
  92. if(convertView==null){
  93. text=newTextView(mContext);
  94. }else{
  95. text=(TextView)convertView;
  96. }
  97. text.setTextColor(Color.WHITE);
  98. text.setText(mList.get(position));
  99. text.setLayoutParams(newGallery.LayoutParams(102,40));
  100. text.setGravity(Gravity.CENTER);
  101. if(position==mSelectedPos)
  102. text.setBackgroundResource(R.drawable.tab_button_select);
  103. else
  104. text.setBackgroundResource(R.drawable.tab_button_unselect);
  105. returntext;
  106. }
  107. }
  108. }


运行结果:
Android 使用Gallery实现Tab_第1张图片

更多相关文章

  1. Android 功能代码总结
  2. android 分享到 代码
  3. Android中NFC功能流程图解析及代码演示『轉』
  4. android 关于 发送post请求的代码总结(包含加上参数)
  5. Android Audio代码分析12 - stream type续
  6. android代码常识

随机推荐

  1. 获取Android的CPU型号
  2. Android不可错过的十大精选网站
  3. (转载)关于android应用程序的入口Activit
  4. android gridview几个重要属性
  5. 一步一步学android之事件篇——触摸事件
  6. 设置Android 模拟器通过代理上网
  7. android XML 解析代码
  8. Android显示SD卡上的图片
  9. 使用ActivityGroup类显示多个Activity
  10. Android在Eclipse下安装开发笔记