当前位置:   article > 正文

ListView条目点击时和点击后图片颜色、文字颜色、条目背景更改大全_修改qlistview::item鼠标移入和点击的背景颜色

修改qlistview::item鼠标移入和点击的背景颜色

当ListView条目点击时,往往需要改变条目里面控件的颜色,如改变文字颜色、条目的背景颜色或是图片颜色。

  1. 而这些颜色的改变又分三大类为:
  2. 1.点击时颜色改变,点击后颜色还原
  3. 2.点击时颜色不变,点击后颜色改变,点击别的条目时颜色还原
  4. 3.点击时颜色改变,点击后颜色保持不变,点击别的条目时颜色还原

而在网上查找的都是零零散散,而且不很详细。于是,现在就总结一下,和大家分享。


首先把框架搭好,因为像这样的颜色改变,ListView放在侧边菜单栏的比较多。而在完整页面中展示数据信息的ListView通常是点击后,将TextView文字颜色改为灰色,标记为已读。而这种实现与Json数据数据存储相关,简单的说就是把条目上对应的Json字段的id存储下来,在getView()方法里更改TextView的颜色,实现局部刷新。所以此方法在这里不实现,以后总结

方便起见,还是按照上一篇博客的知乎框架为案例:

9.在ManiActivity中编写代码,展示ListView

为了显示重点,前面的八步放在了最后面

  1. package com.example.acer.zhihu;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.support.v4.widget.DrawerLayout;
  5. import android.support.v7.app.ActionBarDrawerToggle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.Toolbar;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.BaseAdapter;
  11. import android.widget.ImageView;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14. public class MainActivity extends AppCompatActivity {
  15. private Toolbar mToolbar;
  16. private DrawerLayout mDrawerLayout;
  17. private int[] imagesId = {R.drawable.rb_home, R.drawable.rb_find, R.drawable.rb_focus,
  18. R.drawable.rb_collection, R.drawable.rb_draft, R.drawable.rb_question};
  19. private String[] titles = {"首页", "发现", "关注", "收藏", "草稿", "提问"};
  20. private ListView mLv_titles;
  21. private MyAdapter mMyAdapter;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. // 初始化控件
  27. initView();
  28. // 初始化数据
  29. initData();
  30. }
  31. // 初始化对象
  32. private void initView() {
  33. // 初始化Toolbar、DrawerLayout,生成相应的对象
  34. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  35. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
  36. // 初始化ListView
  37. mLv_titles = (ListView) findViewById(R.id.lv_title);
  38. mMyAdapter = new MyAdapter();
  39. mLv_titles.setAdapter(mMyAdapter);
  40. }
  41. // 设置应用title
  42. private void initData() {
  43. // 设置Toolbar标题,需在setSupportActionBar之前,不然会失效
  44. mToolbar.setTitle("首页");
  45. mToolbar.setTitleTextColor(Color.TRANSPARENT);
  46. // 设置toolbar支持actionbar
  47. setSupportActionBar(mToolbar);
  48. // 实现按钮开关的显示及打开关闭功能并同步动画
  49. initDrawerToggle();
  50. }
  51. private void initDrawerToggle() {
  52. // 参数:开启抽屉的activity、DrawerLayout的对象、toolbar按钮打开关闭的对象、描述open drawer、描述close drawer
  53. ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
  54. // 添加抽屉按钮,通过点击按钮实现打开和关闭功能; 如果不想要抽屉按钮,只允许在侧边边界拉出侧边栏,可以不写此行代码
  55. drawerToggle.syncState();
  56. // 设置按钮的动画效果; 如果不想要打开关闭抽屉时的箭头动画效果,可以不写此行代码
  57. mDrawerLayout.setDrawerListener(drawerToggle);
  58. }
  59. class MyAdapter extends BaseAdapter {
  60. @Override
  61. public int getCount() {
  62. return titles.length;
  63. }
  64. @Override
  65. public Object getItem(int position) {
  66. return titles[position];
  67. }
  68. @Override
  69. public long getItemId(int position) {
  70. return position;
  71. }
  72. @Override
  73. public View getView(int position, View convertView, ViewGroup parent) {
  74. View view = View.inflate(getApplicationContext(), R.layout.left_list, null);
  75. ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
  76. TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
  77. iv_photo.setBackgroundResource(imagesId[position]);
  78. tv_title.setText(titles[position]);
  79. return view;
  80. }
  81. }
  82. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109

效果图: 
这里写图片描述


现在开始更改颜色:

更改文字颜色1:点击时成白色,点击后还原成黑色(通过颜色选择器实现)

在res目录下创建color目录,再创建text_color_select.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:color="#fff" android:state_pressed="true"/>
  4. <item android:color="#000"/>
  5. </selector>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
left_list.xml文件中将:
android:textColor="#000"改为android:textColor="@color/text_color_select"
 
 
  • 1
  • 1

效果动态图: 
这里写图片描述

更改文字颜色2:点击时颜色不变,点击后颜色改变,点击别的条目时颜色还原(状态选择器+条目点击事件+更新位置)

在text_color_select.xml文件中将pressed改为enable

在MainActivity中添加代码:

  1. // 默认当前被选中的item的位置为0
  2. private int mCurrentPos = 0;
  3. ...
  4. // 初始化对象
  5. private void initView() {
  6. // 初始化Toolbar、DrawerLayout,生成相应的对象
  7. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  8. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
  9. // 初始化ListView
  10. mLv_titles = (ListView) findViewById(R.id.lv_title);
  11. mMyAdapter = new MyAdapter();
  12. mLv_titles.setAdapter(mMyAdapter);
  13. mLv_titles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  14. @Override
  15. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  16. // 更新当前被选中的位置
  17. mCurrentPos = position;
  18. // 刷新listview
  19. mMyAdapter.notifyDataSetChanged();
  20. }
  21. });
  22. }
  23. ...
  24. @Override
  25. public View getView(int position, View convertView, ViewGroup parent) {
  26. View view = View.inflate(getApplicationContext(), R.layout.left_list, null);
  27. ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
  28. TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
  29. iv_photo.setBackgroundResource(imagesId[position]);
  30. tv_title.setText(titles[position]);
  31. // 只有当更新的位置等于当前位置时,更改颜色
  32. if(mCurrentPos == position){
  33. tv_title.setEnabled(true);
  34. } else {
  35. tv_title.setEnabled(false);
  36. }
  37. return view;
  38. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

动态效果图: 
这里写图片描述

更改文字颜色3:点击时颜色改变,点击后颜色保持不变,点击别的条目时颜色还原(上诉的两个结合)

  1. 只需要在text_color_select.xml文件中添加一行原被修改的代码:
  2. <item android:color="#fff" android:state_pressed="true"/>

效果图: 
这里写图片描述


更改条目颜色1:点击时成浅蓝色,点击后还原成无色(listSelector+点击刷新)

(为了避免造成干扰,将之前改变TextView颜色的代码全部清除)

在custom_drawer.xml文件的ListView控件中插入:

设置listSelector颜色--android:listSelector="#1C86EE"
 
 
  • 1
  • 1

在MainActivity中设置条目点击事件,刷新ListView即可

  1. mLv_titles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  4. mMyAdapter.notifyDataSetChanged();
  5. }
  6. });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

此方法4.4版本有效运行,5.0版本效果是更改条目颜色3的效果

更改条目颜色2:点击时颜色不变,点击后颜色改变,点击别的条目时颜色还原()

在left_list.xml文件中,给最外层RelativeLayout添加id

android:id="@+id/rl_list_background"
 
 
  • 1
  • 1

在MainActivity中添加代码,和更改文字颜色2非常相似

  1. mLv_titles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  4. // 更新当前被选中的位置
  5. mCurrentPos = position;
  6. // 刷新listview
  7. mMyAdapter.notifyDataSetChanged();
  8. }
  9. });
  10. @Override
  11. public View getView(int position, View convertView, ViewGroup parent) {
  12. View view = View.inflate(getApplicationContext(), R.layout.left_list, null);
  13. ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
  14. TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
  15. mRl_list_background = (RelativeLayout) view.findViewById(R.id.rl_list_background);
  16. iv_photo.setBackgroundResource(imagesId[position]);
  17. tv_title.setText(titles[position]);
  18. // 只有当更新的位置等于当前位置时,更改颜色
  19. if(mCurrentPos == position){
  20. rl_list_background.setBackgroundColor(Color.rgb(35, 154, 237));
  21. } else {
  22. mRl_list_background.setBackgroundColor(Color.TRANSPARENT);
  23. }
  24. return view;
  25. }
  26. ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

效果图: 
这里写图片描述

更改条目颜色3:点击时颜色改变,点击后颜色保持不变,点击别的条目时颜色还原(listSelector)

只需一行代码,就是这么简单--android:listSelector="#1C86EE"
 
 
  • 1
  • 1

效果图: 
这里写图片描述


更改ImageView图片颜色:点击时不变,点击后改变,切换时还原

  1. // 添加图片数组
  2. private int[] imagesEnableId =
  3. {R.drawable.rb_home_enable, R.drawable.rb_find_enable, R.drawable.rb_focus_enable,
  4. R.drawable.rb_collection_enable, R.drawable.rb_draft_enable, R.drawable.rb_question_enable};
  5. ...
  6. private void initView() {
  7. // 初始化Toolbar、DrawerLayout,生成相应的对象
  8. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  9. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
  10. // 初始化ListView
  11. mLv_titles = (ListView) findViewById(R.id.lv_title);
  12. mMyAdapter = new MyAdapter();
  13. mLv_titles.setAdapter(mMyAdapter);
  14. mLv_titles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  15. @Override
  16. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  17. // 更新当前被选中的位置
  18. mCurrentPos = position;
  19. // 刷新listview
  20. mMyAdapter.notifyDataSetChanged();
  21. }
  22. });
  23. }
  24. ...
  25. @Override
  26. public View getView(int position, View convertView, ViewGroup parent) {
  27. View view = View.inflate(getApplicationContext(), R.layout.left_list, null);
  28. ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
  29. TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
  30. mRl_list_background = (RelativeLayout) view.findViewById(R.id.rl_list_background);
  31. System.out.println("------条目点击后");
  32. iv_photo.setBackgroundResource(imagesId[position]);
  33. tv_title.setText(titles[position]);
  34. // 只有当更新的位置等于当前位置时,更改颜色
  35. if(mCurrentPos == position){
  36. mRl_list_background.setBackgroundColor(Color.rgb(35, 154, 237));
  37. iv_photo.setBackgroundResource(imagesEnableId[position]);
  38. } else {
  39. mRl_list_background.setBackgroundColor(Color.TRANSPARENT);
  40. iv_photo.setBackgroundResource(imagesId[position]);
  41. }
  42. return view;
  43. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

效果图: 
这里写图片描述


1.创建项目,检查是否有v7包

  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. testCompile 'junit:junit:4.12'
  4. compile 'com.android.support:appcompat-v7:24.2.1'
  5. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.修改styles的theme样式

  1. <resources>
  2. <resources>
  3. <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
  4. <!-- Customize your theme here. -->
  5. <item name="colorPrimary">@color/colorPrimary</item>
  6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  7. <item name="colorAccent">@color/colorAccent</item>
  8. <!--返回键样式-->
  9. <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
  10. <item name="windowActionBar">false</item>
  11. <item name="android:windowNoTitle">true</item>
  12. <!-- 使用 API Level 22编译的話,要拿掉前綴字 -->
  13. <item name="windowNoTitle">true</item>
  14. </style>
  15. <!-- Base application theme. -->
  16. <style name="AppTheme" parent="AppTheme.Base"></style>
  17. <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
  18. <item name="color">@android:color/white</item>
  19. </style>
  20. </resources>
  21. </resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.给strings.xml文件添加值

  1. <resources>
  2. <string name="app_name">ZhiHu</string>
  3. <string name="open">open</string>
  4. <string name="close">close</string>
  5. </resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

4.创建ToolBar标题栏的布局文件:tool_bar.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/toolbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:background="?attr/colorPrimary"
  7. android:minHeight="?attr/actionBarSize">
  8. </android.support.v7.widget.Toolbar>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

前四步一模一样,在这里粘贴出来是为了让大家方便看代码,如有不解,请参考上一篇博文

5.创建DrawerLayout侧滑面板的布局:custom_drawer.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/drawerLayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!--主布局,ToolBar下面的布局-->
  7. <RelativeLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="首页"
  14. android:textSize="30dp"
  15. android:textColor="@android:color/darker_gray"
  16. android:layout_centerInParent="true"/>
  17. </RelativeLayout>
  18. <!--侧滑菜单,左边隐藏的布局-->
  19. <RelativeLayout
  20. android:id="@+id/rl_left_content"
  21. android:layout_width="250dp"
  22. android:layout_height="match_parent"
  23. android:background="#F5F5F5"
  24. android:orientation="vertical"
  25. android:layout_gravity="start">
  26. <RelativeLayout
  27. android:id="@+id/rl_photo"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:orientation="horizontal"
  31. android:padding="12dp">
  32. <ImageButton
  33. android:id="@+id/ib_photo"
  34. android:layout_width="40dp"
  35. android:layout_height="40dp"
  36. android:background="@drawable/ic_launcher"
  37. android:scaleType="center"/>
  38. <TextView
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_toRightOf="@id/ib_photo"
  42. android:layout_marginLeft="10dp"
  43. android:layout_centerVertical="true"
  44. android:text="昵称"
  45. android:textColor="#000"
  46. android:textSize="20sp"/>
  47. </RelativeLayout>
  48. <!--点击条目时显示的颜色android:listSelector="#1C86EE"-->
  49. <ListView
  50. android:id="@+id/lv_title"
  51. android:layout_below="@id/rl_photo"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:divider="@null"/>
  55. <!--画一条横线分隔开-->
  56. <View
  57. android:layout_above="@+id/rl_setting"
  58. android:layout_width="match_parent"
  59. android:layout_height="1px"
  60. android:background="#888"/>
  61. <RelativeLayout
  62. android:id="@+id/rl_setting"
  63. android:layout_above="@+id/rl_theme"
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:padding="15dp">
  67. <TextView
  68. android:text="设置"
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:layout_centerVertical="true"
  72. android:textColor="#8000"
  73. android:textSize="15sp"/>
  74. </RelativeLayout>
  75. <RelativeLayout
  76. android:id="@+id/rl_theme"
  77. android:layout_alignParentBottom="true"
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:padding="15dp">
  81. <TextView
  82. android:text="切换主题"
  83. android:layout_width="match_parent"
  84. android:layout_height="wrap_content"
  85. android:layout_centerVertical="true"
  86. android:textColor="#8000"
  87. android:textSize="15sp"/>
  88. </RelativeLayout>
  89. </RelativeLayout>
  90. </android.support.v4.widget.DrawerLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

6.在activity.xml文件中插入ToolBar布局、DrawerLayout布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <!--Toolbar-->
  9. <include layout="@layout/tool_bar" />
  10. <!--DrawerLayout-->
  11. <include layout="@layout/custom_drawer" />
  12. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

7.修改colors.xml标题栏颜色

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="colorPrimary">#1C86EE</color>
  4. <color name="colorPrimaryDark">#000000</color>
  5. <color name="colorAccent">#FF4081</color>
  6. </resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

8.给ListView创建left_list.xml布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal"
  6. android:padding="13dp">
  7. <ImageView
  8. android:id="@+id/iv_photo"
  9. android:layout_width="25dp"
  10. android:layout_height="25dp"
  11. android:scaleType="center"/>
  12. <TextView
  13. android:text="首页"
  14. android:id="@+id/tv_title"
  15. android:layout_toRightOf="@id/iv_photo"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="15dp"
  19. android:layout_centerVertical="true"
  20. android:textColor="#000"
  21. android:textSize="18sp"/>
  22. </RelativeLayout>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/170821
推荐阅读
相关标签
  

闽ICP备14008679号