Android在题栏加按钮.docx

上传人:牧羊曲112 文档编号:3152396 上传时间:2023-03-11 格式:DOCX 页数:13 大小:39.65KB
返回 下载 相关 举报
Android在题栏加按钮.docx_第1页
第1页 / 共13页
Android在题栏加按钮.docx_第2页
第2页 / 共13页
Android在题栏加按钮.docx_第3页
第3页 / 共13页
Android在题栏加按钮.docx_第4页
第4页 / 共13页
Android在题栏加按钮.docx_第5页
第5页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Android在题栏加按钮.docx》由会员分享,可在线阅读,更多相关《Android在题栏加按钮.docx(13页珍藏版)》请在三一办公上搜索。

1、Android在题栏加按钮Android中标题栏添加按钮 现在很多的Android程序都在标题栏上都显示了一些按钮和标题,如下图: 下面通过实例来看一下如何实现。 1、在layout下创建一个titlebtn.xml文件,内容如下: html view plaincopyprint? 1. 2. 6. 7. 15. 16. 21. 22. 30. 31. ?xml version=1.0 encoding=uRelativeLayout xmlns:android= android:orientation=horizo android:layout_width=fill_ android:la

2、yout_height=fill 在创建这个xml时需要注意: a)使用RelativeLayout的布局 b)特别是右边按钮的属性需要指定layout_centerInParent 2、在Activity中的onCreate中增加下面的代码: java view plaincopyprint? 1. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 2. setContentView(R.layout.main); 3. getWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layou

3、t.titlebtn); requestWindowFeature(Wi setContentView(R.layout getWindow.setFeatureI通过上面的两个步骤就可以实现了上面的效果了。 说一下具体步骤。首先,修改标题栏的宽度和背景,在strings.xml中添加: drawable/title_bg 40dp style/CustomWindowTitleBackground 然后修改AndroidMainfest.xml文件: 红色部分为加载Activity的theme。 接着修改MainActivity: public void onCreate(Bundle sa

4、vedInstanceState) super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); /声明使用自定义标题 setContentView(R.layout.main); getWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);/自定义布局赋值 注意红色部分标记的3行代码,顺序不能颠倒。 最后在layout文件夹中添加title.xml文件,内容如下: Main.java java view

5、plaincopyprint? 1. package com.JavaTiger; 2. 3. import android.app.Activity; 4. import android.app.AlertDialog; 5. import android.content.DialogInterface; 6. import android.content.Intent; 7. import android.os.Bundle; 8. import android.view.KeyEvent; 9. import android.view.View; 10. import android.v

6、iew.Window; 11. import android.view.View.OnClickListener; 12. import android.widget.Button; 13. import android.widget.TextView; 14. 15. public class Main extends Activity 16. Override 17. public void onCreate(Bundle savedInstanceState) 18. setTheme(R.style.CustomTheme); 19. super.onCreate(savedInsta

7、nceState); 20. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 21. setContentView(R.layout.main); 22. getWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); 23. tvTitle = (TextView) findViewById(R.id.Titletext); 24. tvTitle.setText(标题栏返回按钮测试界面1); 25. 26. btnBack = (Button) findView

8、ById(R.id.TitleBackBtn); 27. 28. btnBack.setOnClickListener(new OnClickListener 29. public void onClick(View v) 30. KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, 31. KeyEvent.KEYCODE_BACK); 32. onKeyDown(KeyEvent.KEYCODE_BACK, newEvent); 33. 34. ); 35. 36. btnNext = (Button) findViewById(R.

9、id.Button); 37. 38. btnNext.setOnClickListener(new OnClickListener 39. public void onClick(View v) 40. Intent intent = new Intent; 41. intent.setClass(Main.this, Next.class); 42. startActivity(intent); 43. 44. ); 45. 46. 47. Override 48. public boolean onKeyDown(int keyCode, KeyEvent event) 49. if (

10、keyCode = KeyEvent.KEYCODE_BACK & event.getRepeatCount = 0) 50. / 按下的如果是BACK,同时没有重复 51. askForOut; 52. 53. return true; 54. 55. 56. return super.onKeyDown(keyCode, event); 57. 58. 59. private void askForOut 60. AlertDialog.Builder builder = new AlertDialog.Builder(this); 61. 62. builder.setTitle(确定退

11、出).setMessage(确定退出?).setPositiveButton(确定, 63. new DialogInterface.OnClickListener 64. Override 65. public void onClick(DialogInterface dialog, int which) 66. finish; 67. 68. ).setNegativeButton(取消, 69. new DialogInterface.OnClickListener 70. Override 71. public void onClick(DialogInterface dialog,

12、int which) 72. dialog.cancel; 73. 74. ).setCancelable(false).show; 75. 76. 77. public TextView tvTitle; 78. public Button btnBack; 79. public Button btnNext; 80. package com.JavaTiger;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInimport android.content.Int

13、ent;注意:上面代码中红色代码的顺序,不能随便改变,一定要按照次顺序,否则会报错! Next.java java view plaincopyprint? 1. package com.JavaTiger; 2. 3. import android.app.Activity; 4. import android.os.Bundle; 5. import android.view.KeyEvent; 6. import android.view.View; 7. import android.view.Window; 8. import android.view.View.OnClickLis

14、tener; 9. import android.widget.Button; 10. import android.widget.TextView; 11. 12. public class Next extends Activity 13. Override 14. public void onCreate(Bundle savedInstanceState) 15. setTheme(R.style.CustomTheme); 16. super.onCreate(savedInstanceState); 17. requestWindowFeature(Window.FEATURE_C

15、USTOM_TITLE); 18. setContentView(R.layout.next); 19. getWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); 20. 21. tvTitle = (TextView) findViewById(R.id.Titletext); 22. 23. tvTitle.setText(标题栏返回按钮测试界面2); 24. 25. btnBack = (Button) findViewById(R.id.TitleBackBtn); 26. 27. btnBack.set

16、OnClickListener(new OnClickListener 28. public void onClick(View v) 29. KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, 30. KeyEvent.KEYCODE_BACK); 31. onKeyDown(KeyEvent.KEYCODE_BACK, newEvent); 32. 33. ); 34. 35. 36. public Button btnBack; 37. public TextView tvTitle; 38. package com.JavaTi

17、ger;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;xml布局文件: style.xml html view plaincopyprint? 1. 2. 3. 4. 40dip 5. 6. ?xml version=1.0 encoding=u style name=CustomThemitem name=and title.xml html view plaincopyprint? 1. 2. 5. 14. 22. ?xml

18、 version=1.0 encoding=uRelativeLayout xmlns:android=android:layout_width=f android:layout_height= TextView android:id=+imain.xml html view plaincopyprint? 1. 2. 6. 10. 15. ?xml version=1.0 encoding=uLinearLayout xmlns:android=ht android:orientation=vertic android:layout_width=fill_ android:layout_height=fillTextViewnext.xml html view plaincopyprint? 1. 2. 6. 10. ?xml version=1.0 encoding=uLinearLayout xmlns:android=ht android:orientation=vertic android:layout_width=fill_ android:layout_height=fillTextView欲行效果图如下:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号