Android 高级控件

Bertha 。 2022-09-24 13:30 262阅读 0赞

高级控件:

GridView

ScrollView
ViewPager
SlideMenu

PullToRefreshListView

1.ScrollView视图(滚动视图)可以有效的安排这些组件,浏览时可以进行滚屏的操作
垂直滚动——ScrollView
水平滚动——HorizontalScrollView
ScrollView是单一容器,只能包含一个组件

练习:设置界面的垂直滚动

理想效果 完成效果

CenterCenter 1

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ScrollView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical"
  12. >
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:orientation="horizontal"
  17. android:layout_marginLeft="20dp"
  18. >
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:textSize="30sp"
  23. />
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="设置"
  28. android:textSize="30sp"
  29. android:layout_marginLeft="100dp"
  30. />
  31. <TextView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="意见反馈"
  35. android:layout_gravity="center"
  36. android:layout_marginLeft="100dp"
  37. />
  38. </LinearLayout>
  39. <TextView
  40. android:layout_width="match_parent"
  41. android:layout_height="1dp"
  42. android:background="#d9d3d3"
  43. />
  44. <LinearLayout
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:orientation="horizontal"
  48. android:layout_marginLeft="20dp"
  49. android:layout_marginTop="10dp"
  50. >
  51. <TextView
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:text="列表显示摘要"
  55. android:textSize="20sp"
  56. />
  57. <CheckBox
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_marginLeft="170dp"
  61. />
  62. </LinearLayout>
  63. <TextView
  64. android:layout_width="match_parent"
  65. android:layout_height="1dp"
  66. android:background="#d9d3d3"
  67. />
  68. <LinearLayout
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:orientation="horizontal"
  72. android:layout_marginLeft="20dp"
  73. android:layout_marginTop="10dp"
  74. >
  75. <TextView
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:text="字体大小"
  79. android:textSize="20sp"
  80. />
  81. <TextView
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:text="中"
  85. android:layout_marginLeft="220dp"
  86. />
  87. <TextView
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:text=">"
  91. android:textSize="20sp"
  92. android:layout_marginLeft="10dp"
  93. />
  94. </LinearLayout>
  95. <TextView
  96. android:layout_width="match_parent"
  97. android:layout_height="1dp"
  98. android:background="#d9d3d3"
  99. />
  100. <LinearLayout
  101. android:layout_width="wrap_content"
  102. android:layout_height="wrap_content"
  103. android:orientation="horizontal"
  104. android:layout_marginLeft="20dp"
  105. android:layout_marginTop="10dp"
  106. >
  107. <TextView
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:text="列表页评论"
  111. android:textSize="20sp"
  112. />
  113. <TextView
  114. android:layout_width="wrap_content"
  115. android:layout_height="wrap_content"
  116. android:text="不限"
  117. android:layout_marginLeft="190dp"
  118. />
  119. <TextView
  120. android:layout_width="wrap_content"
  121. android:layout_height="wrap_content"
  122. android:text=">"
  123. android:textSize="20sp"
  124. android:layout_marginLeft="10dp"
  125. />
  126. </LinearLayout>
  127. <TextView
  128. android:layout_width="match_parent"
  129. android:layout_height="1dp"
  130. android:background="#d9d3d3"
  131. />
  132. <LinearLayout
  133. android:layout_width="wrap_content"
  134. android:layout_height="wrap_content"
  135. android:orientation="horizontal"
  136. android:layout_marginLeft="20dp"
  137. android:layout_marginTop="10dp"
  138. >
  139. <TextView
  140. android:layout_width="wrap_content"
  141. android:layout_height="wrap_content"
  142. android:text="2G/3G网络流量"
  143. android:textSize="20sp"
  144. />
  145. <TextView
  146. android:layout_width="wrap_content"
  147. android:layout_height="wrap_content"
  148. android:text="较省流量(智能下图)"
  149. android:layout_gravity="center"
  150. android:layout_marginLeft="50dp"
  151. />
  152. <TextView
  153. android:layout_width="wrap_content"
  154. android:layout_height="wrap_content"
  155. android:text=">"
  156. android:textSize="20sp"
  157. android:layout_marginLeft="1dp"
  158. />
  159. </LinearLayout>
  160. <TextView
  161. android:layout_width="match_parent"
  162. android:layout_height="1dp"
  163. android:background="#d9d3d3"
  164. />
  165. <LinearLayout
  166. android:layout_width="wrap_content"
  167. android:layout_height="wrap_content"
  168. android:orientation="horizontal"
  169. android:layout_marginLeft="20dp"
  170. android:layout_marginTop="10dp"
  171. >
  172. <TextView
  173. android:layout_width="wrap_content"
  174. android:layout_height="wrap_content"
  175. android:text="清理缓存"
  176. android:textSize="20sp"
  177. />
  178. <TextView
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. android:text="当前缓存:2.48MB"
  182. android:layout_gravity="center"
  183. android:layout_marginLeft="120dp"
  184. />
  185. <TextView
  186. android:layout_width="wrap_content"
  187. android:layout_height="wrap_content"
  188. android:text=">"
  189. android:textSize="20sp"
  190. android:layout_marginLeft="10dp"
  191. />
  192. </LinearLayout>
  193. <TextView
  194. android:layout_width="match_parent"
  195. android:layout_height="1dp"
  196. android:background="#d9d3d3"
  197. />
  198. <LinearLayout
  199. android:layout_width="wrap_content"
  200. android:layout_height="wrap_content"
  201. android:orientation="horizontal"
  202. android:layout_marginLeft="20dp"
  203. android:layout_marginTop="30dp"
  204. >
  205. <TextView
  206. android:layout_width="wrap_content"
  207. android:layout_height="wrap_content"
  208. android:text="推送通知"
  209. android:textSize="20sp"
  210. />
  211. <CheckBox
  212. android:layout_width="wrap_content"
  213. android:layout_height="wrap_content"
  214. android:checked="true"
  215. android:layout_marginLeft="210dp"
  216. />
  217. </LinearLayout>
  218. <TextView
  219. android:layout_width="match_parent"
  220. android:layout_height="1dp"
  221. android:background="#d9d3d3"
  222. />
  223. <LinearLayout
  224. android:layout_width="wrap_content"
  225. android:layout_height="wrap_content"
  226. android:orientation="horizontal"
  227. android:layout_marginLeft="20dp"
  228. android:layout_marginTop="10dp"
  229. >
  230. <TextView
  231. android:layout_width="wrap_content"
  232. android:layout_height="wrap_content"
  233. android:text="互动插件"
  234. android:textSize="20sp"
  235. />
  236. <CheckBox
  237. android:layout_width="wrap_content"
  238. android:layout_height="wrap_content"
  239. android:checked="true"
  240. android:layout_marginLeft="210dp"
  241. />
  242. </LinearLayout>
  243. <TextView
  244. android:layout_width="match_parent"
  245. android:layout_height="1dp"
  246. android:background="#d9d3d3"
  247. />
  248. <LinearLayout
  249. android:layout_width="wrap_content"
  250. android:layout_height="wrap_content"
  251. android:orientation="horizontal"
  252. android:layout_marginLeft="20dp"
  253. android:layout_marginTop="10dp"
  254. >
  255. <TextView
  256. android:layout_width="wrap_content"
  257. android:layout_height="wrap_content"
  258. android:text="自动优化阅读"
  259. android:textSize="20sp"
  260. />
  261. <CheckBox
  262. android:layout_width="wrap_content"
  263. android:layout_height="wrap_content"
  264. android:checked="true"
  265. android:layout_marginLeft="170dp"
  266. />
  267. </LinearLayout>
  268. <TextView
  269. android:layout_width="match_parent"
  270. android:layout_height="1dp"
  271. android:background="#d9d3d3"
  272. />
  273. <LinearLayout
  274. android:layout_width="wrap_content"
  275. android:layout_height="wrap_content"
  276. android:orientation="horizontal"
  277. android:layout_marginLeft="20dp"
  278. android:layout_marginTop="10dp"
  279. >
  280. <TextView
  281. android:layout_width="wrap_content"
  282. android:layout_height="wrap_content"
  283. android:text="收藏时转发"
  284. android:textSize="20sp"
  285. />
  286. <CheckBox
  287. android:layout_width="wrap_content"
  288. android:layout_height="wrap_content"
  289. android:layout_marginLeft="190dp"
  290. />
  291. </LinearLayout>
  292. <TextView
  293. android:layout_width="match_parent"
  294. android:layout_height="1dp"
  295. android:background="#d9d3d3"
  296. />
  297. <LinearLayout
  298. android:layout_width="wrap_content"
  299. android:layout_height="wrap_content"
  300. android:orientation="horizontal"
  301. android:layout_marginLeft="20dp"
  302. android:layout_marginTop="10dp"
  303. >
  304. <TextView
  305. android:layout_width="wrap_content"
  306. android:layout_height="wrap_content"
  307. android:text="顶踩时转发"
  308. android:textSize="20sp"
  309. />
  310. <CheckBox
  311. android:layout_width="wrap_content"
  312. android:layout_height="wrap_content"
  313. android:layout_marginLeft="190dp"
  314. />
  315. </LinearLayout>
  316. <TextView
  317. android:layout_width="match_parent"
  318. android:layout_height="1dp"
  319. android:background="#d9d3d3"
  320. />
  321. </LinearLayout>
  322. </ScrollView>
  323. </RelativeLayout>

2.GridView常用属性

android:numColumns=”auto_fit” //GridView的列数设置为自动
android:columnWidth=”90dp “ //每列的宽度,也就是Item的宽度
android:stretchMode=”columnWidth“//设置缩放模式,与列宽大小同步
android:verticalSpacing=”10dp” //两行之间的边距
android:horizontalSpacing=”10dp” //两列之间的边距
android:cacheColorHint=”#00000000” //去除拖动时默认的黑色背景
android:listSelector=”#00000000” //去除选中时的黄色底色
android:scrollbars=”none” //隐藏GridView的滚动条

练习:利用GridView完成如下图所示效果

理想效果 完成效果

Center 2Center 3

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.administrator.jreduch05.GridView_Activity">
  7. <GridView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:id="@+id/gv"
  11. android:numColumns="auto_fit"
  12. android:columnWidth="100dp"
  13. android:horizontalSpacing="5dp"
  14. android:verticalSpacing="5dp"
  15. android:cacheColorHint="#00000000"
  16. android:listSelector="#00000000"
  17. android:scrollbars="none"
  18. android:stretchMode="columnWidth"
  19. >
  20. </GridView>
  21. </RelativeLayout>
  22. <?xml version="1.0" encoding="utf-8"?>
  23. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  24. android:orientation="vertical" android:layout_width="match_parent"
  25. android:layout_height="match_parent">
  26. <ImageView
  27. android:layout_width="100dp"
  28. android:layout_height="100dp"
  29. android:id="@+id/iv"
  30. android:src="@mipmap/ic_launcher"
  31. android:scaleType="centerCrop"
  32. />
  33. <TextView
  34. android:layout_width="100dp"
  35. android:layout_height="wrap_content"
  36. android:id="@+id/tv"
  37. android:gravity="center"
  38. android:text="测试"
  39. />
  40. </LinearLayout>

代码:

  1. </pre><pre name="code" class="html">package com.example.administrator.jreduch05;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.GridView;
  8. import android.widget.SimpleAdapter;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class GridView_Activity extends AppCompatActivity {
  14. private GridView gv;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_grid_view_);
  19. gv=(GridView) findViewById(R.id.gv);
  20. final List list =new ArrayList();
  21. Map map=new HashMap();
  22. map.put("img", R.mipmap.dahai);
  23. map.put("name", "深海");
  24. list.add(map);
  25. map=new HashMap();
  26. map.put("img",R.mipmap.jietu1);
  27. map.put("name","深海");
  28. list.add(map);
  29. map=new HashMap();
  30. map.put("img",R.mipmap.dianzan);
  31. map.put("name","深海");
  32. list.add(map);
  33. map=new HashMap();
  34. map.put("img",R.mipmap.pinglun);
  35. map.put("name","深海");
  36. list.add(map);
  37. map=new HashMap();
  38. map.put("img",R.mipmap.renrou);
  39. map.put("name","深海");
  40. list.add(map);
  41. map=new HashMap();
  42. map.put("img",R.mipmap.sannv);
  43. map.put("name","深海");
  44. list.add(map);
  45. map=new HashMap();
  46. map.put("img",R.mipmap.wuzhi);
  47. map.put("name","深海");
  48. list.add(map);
  49. /*
  50. 1.simpleAdapter 使用的数据源必须继承 Map 接口
  51. 2.from 参数的意思是 指向数据源 Map中的键
  52. 3.to 参数的意思是 为布局中控件ID赋值
  53. */
  54. SimpleAdapter sa=new SimpleAdapter(this,list,
  55. R.layout.gridlayout,
  56. new String[]{"img","name"},
  57. new int[]{R.id.iv,R.id.tv}
  58. );
  59. gv.setAdapter(sa);
  60. gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  61. @Override
  62. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  63. Map map=(Map)list.get(position);
  64. Intent intent=new Intent(GridView_Activity.this,Datu_Activity.class);
  65. intent.putExtra("img", (int) map.get("img"));
  66. startActivity(intent);
  67. }
  68. });
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读