main.xml布局文件:
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "vertical" android:layout_width="fill_parent"
4. "fill_parent">
5. "@+id/gallery" android:layout_width="fill_parent"
6. "wrap_content" android:layout_marginTop="30dp"
7. "1" android:spacing="1dip"
8. </LinearLayout>
values/attrs.xml:
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
3. "Gallery">
4. "android:galleryItemBackground"
5. </declare-styleable>
6. </resources>
values/strings.xml:
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
3. "hello">Hello World, Date!</string>
4. "app_name">丸子-Widget</string>
5. </resources>
drawable/tab_button_select.xml:
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
3. "#FF1B1B1B" android:endColor="#FF969696"
4. "90.0">
5. </gradient>
6. </shape>
drawable/tab_button_unselect.xml:
Java代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
3. "#FF000000" android:endColor="#FF474747"
4. "90.0">
5. </gradient>
6. </shape>
IaiaiActivity.java类:
Java代码
1. package
2.
3. import
4. import
5. import
6.
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20.
21. /**
22. *
23. * <p>
24. * Title: IaiaiActivity.java
25. * </p>
26. * <p>
27. * E-Mail: [email protected]
28. * </p>
29. * <p>
30. * QQ: 176291935
31. * </p>
32. * <p>
33. * Http: iaiai.iteye.com
34. * </p>
35. * <p>
36. * Create time: 2011-6-26
37. * </p>
38. *
39. * @author 丸子
40. * @version 0.0.1
41. */
42. public class IaiaiActivity extends
43.
44. private
45. private
46.
47. private static final String[] PROGRAM_NAMES = { "中央一台", "中央二台", "中央三台",
48. "中央四台", "中央五台", "中央六台", "中央七台", "中央八台", };
49.
50. @Override
51. public void
52. super.onCreate(savedInstanceState);
53. setContentView(R.layout.main);
54.
55. gallery = (Gallery) findViewById(R.id.gallery);
56. new TabAdapter(this, Arrays.asList(PROGRAM_NAMES));
57. gallery.setAdapter(textAdapter);
58. new
59.
60. @Override
61. public void
62. int position, long
63. TabAdapter adapter = (TabAdapter) parent.getAdapter();
64. adapter.setSelectedPos(position);
65. }
66.
67. });
68. }
69.
70. public class TabAdapter extends
71. private
72. private
73. private int
74.
75. public
76. mContext = context;
77. TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
78. a.recycle();
79. if (list == null)
80. list = Collections.emptyList();
81. mList = list;
82. }
83.
84. public void setSelectedPos(int
85. if
86. mSelectedPos = pos;
87. notifyDataSetChanged();
88. }
89. }
90.
91. public int
92. return
93. }
94.
95. public int
96. return
97. }
98.
99. public Object getItem(int
100. return
101. }
102.
103. public long getItemId(int
104. return
105. }
106.
107. public View getView(int
108. null;
109. if (convertView == null) {
110. new
111. else
112. text = (TextView) convertView;
113. }
114.
115. text.setTextColor(Color.WHITE);
116. text.setText(mList.get(position));
117.
118. new Gallery.LayoutParams(102, 40));
119. text.setGravity(Gravity.CENTER);
120.
121. if
122. text.setBackgroundResource(R.drawable.tab_button_select);
123. else
124. text.setBackgroundResource(R.drawable.tab_button_unselect);
125.
126. return
127. }
128. }
129.
130. }
运行结果: