Android定制属于你自己的导航栏

在实际开发中,我们时常要用到上方的两个按钮,通俗的我们可以叫做导航,等等.还是先看今天需要要实现的一个最的效果:


其实实现这样的效果有多种方式,今天我要给大家要介绍的就是如何的去定制自己的控件,也就是自定义控件,自定义控件分为多种,有组合控件,有重写在原来已有的控件上做基础的修改,也有自己重写写一个类继承于View对象,这方面的知识在实际开发当中也会常碰到,当然像我们这种菜鸟在这方面也是最欠缺的一个知识点,我希望通过我的一些讲解或者分享能帮助到大家吧。今天我给大家讲的就是组合控件的自定义,在后续的博文中我也希望自己能发现更多的这种自定义控件的能力然后再与大家分享,同样如果大家有好的分享的东西也可以与我一起分享。就拿今天的一个效果如何去实现自定义控件,我会采用两种方式去实现,一种就是纯粹的代码方式实现,一种就是用LayoyutInfalter去实现。

先说XML方式的实现方式:

先定义一个XML布局文件:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="45dip"
  5. android:background="@drawable/navigation_bg"
  6. android:gravity="center_vertical"
  7. android:orientation="horizontal">
  8. <Button
  9. android:id="@+id/btn_left"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginLeft="10.0dip"
  13. android:background="@drawable/backbg"
  14. android:text="返回"
  15. android:textColor="@android:color/white"/>
  16. <TextView
  17. android:id="@+id/tv_title"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:gravity="center"
  22. android:singleLine="true"
  23. android:text="标题"
  24. android:textColor="@android:color/white"/>
  25. <Button
  26. android:id="@+id/btn_right"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_marginRight="10.0dip"
  30. android:background="@drawable/buttonbg"
  31. android:text="新增"
  32. android:textColor="@android:color/white"/>
  33. </LinearLayout>

然后我们再重新写一个类继承于LineaLayour。通过在构造方法中去加载这个布局文件,这样就能实现一个组合控件的定义了,这样的方式是不是特别简单。

  1. publicclassUINavigationView2extendsLinearLayout{
  2. privateButtonbtn_left;
  3. privateButtonbtn_right;
  4. privateTextViewtv_title;
  5. publicUINavigationView2(Contextcontext){
  6. super(context);
  7. //TODOAuto-generatedconstructorstub
  8. }
  9. publicUINavigationView2(Contextcontext,AttributeSetattrs){
  10. super(context,attrs);
  11. LayoutInflaterlayoutInflater=(LayoutInflater)context
  12. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  13. layoutInflater.inflate(R.layout.title_bar,null);
  14. btn_left=(Button)findViewById(R.id.btn_left);
  15. btn_right=(Button)findViewById(R.id.btn_right);
  16. tv_title=(Button)findViewById(R.id.tv_title);
  17. }
  18. publicvoidsetBtnLeftBacground(intresId){
  19. if(btn_left!=null){
  20. btn_left.setBackgroundResource(resId);
  21. }
  22. }
  23. publicvoidsetBtnRightBacground(intresId){
  24. if(btn_right!=null){
  25. btn_right.setBackgroundResource(resId);
  26. }
  27. }
  28. publicvoidsetTvTitle(intresId){
  29. if(tv_title!=null){
  30. tv_title.setText(resId);
  31. }
  32. }
  33. publicButtongetBtn_left(){
  34. returnbtn_left;
  35. }
  36. publicButtongetBtn_right(){
  37. returnbtn_right;
  38. }
  39. publicTextViewgetTv_title(){
  40. returntv_title;
  41. }
  42. }

接下来我们再按如果通过代码来定义控件,这里需要了解的一个知识点,如果我们需要在我们的自定义控件如果新添属于我们自己的属性呢?这里我就们就需要attr.xml文件中自己定义,关于这方面的知识大家可以参考这篇文章:http://huangbo-2020.iteye.com/blog/1477611

我们先看我们自己定义的控件定义新增的如下几个属性:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="navigation">
  4. <attrname="btn_leftText"format="string"/>
  5. <attrname="btn_rightText"format="string"/>
  6. <attrname="tv_title"format="string"/>
  7. <attrname="left_drawable"format="reference"></attr>
  8. <attrname="right_drawable"format="reference"></attr>
  9. </declare-styleable>
  10. </resources>

再看我们的自定义控件的代码中如何写的:

  1. publicclassUINavigationViewextendsLinearLayout{
  2. privateButtonbtn_left;
  3. privateButtonbtn_right;
  4. privateTextViewtv_title;
  5. privateStringstrBtnLeft;
  6. privateStringstrBtnRight;
  7. privateStringstrTitle;
  8. privateintleft_drawable;
  9. privateintright_drawable;
  10. publicUINavigationView(Contextcontext){
  11. super(context);
  12. initContent();
  13. }
  14. publicUINavigationView(Contextcontext,AttributeSetattrs){
  15. super(context,attrs);
  16. initAttributes(attrs);
  17. initContent();
  18. }
  19. privatevoidinitAttributes(AttributeSetattributeSet){
  20. if(null!=attributeSet){
  21. finalintattrIds[]=newint[]{R.attr.btn_leftText,
  22. R.attr.btn_rightText,R.attr.tv_title,
  23. R.attr.left_drawable,R.attr.right_drawable};
  24. Contextcontext=getContext();
  25. TypedArrayarray=context.obtainStyledAttributes(attributeSet,
  26. attrIds);
  27. CharSequencet1=array.getText(0);
  28. CharSequencet2=array.getText(1);
  29. CharSequencet3=array.getText(2);
  30. left_drawable=array.getResourceId(3,0);
  31. right_drawable=array.getResourceId(4,0);
  32. array.recycle();
  33. if(null!=t1){
  34. strBtnLeft=t1.toString();
  35. }
  36. if(null!=t2){
  37. strBtnRight=t2.toString();
  38. }
  39. if(null!=t3){
  40. strTitle=t3.toString();
  41. }
  42. Log.i("coder","t1="+t1);
  43. Log.i("coder","t2="+t2);
  44. Log.i("coder","t3="+t3);
  45. Log.i("coder","left_res="+left_drawable);
  46. }
  47. }
  48. privatevoidinitContent(){
  49. Log.i("coder","-----initContent----");
  50. //设置水平方向
  51. setOrientation(HORIZONTAL);
  52. setGravity(Gravity.CENTER_VERTICAL);
  53. //设置背景
  54. setBackgroundResource(R.drawable.navigation_bg);
  55. Contextcontext=getContext();
  56. btn_left=newButton(context);
  57. btn_left.setVisibility(View.INVISIBLE);//设置设置不可见
  58. if(left_drawable!=0){
  59. btn_left.setBackgroundResource(left_drawable);
  60. }else{
  61. btn_left.setBackgroundResource(R.drawable.backbg);//设置背景
  62. }
  63. btn_left.setTextColor(Color.WHITE);//字体颜色
  64. if(null!=strBtnLeft){
  65. LayoutParamsbtnLeftParams=newLayoutParams(
  66. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  67. btnLeftParams.setMargins(10,0,0,0);
  68. btn_left.setLayoutParams(btnLeftParams);
  69. btn_left.setText(strBtnLeft);
  70. btn_left.setVisibility(View.VISIBLE);
  71. }else{
  72. btn_left.setLayoutParams(newLayoutParams(50,50));
  73. }
  74. //添加这个按钮
  75. addView(btn_left);
  76. //
  77. tv_title=newTextView(context);
  78. LayoutParamscenterParam=newLayoutParams(LayoutParams.FILL_PARENT,
  79. LayoutParams.FILL_PARENT);
  80. centerParam.weight=1;
  81. tv_title.setLayoutParams(centerParam);
  82. tv_title.setTextColor(Color.WHITE);
  83. if(null!=strTitle){
  84. tv_title.setText(strTitle);
  85. }
  86. tv_title.setGravity(Gravity.CENTER);
  87. btn_left.setVisibility(View.VISIBLE);
  88. //添加这个标题
  89. addView(tv_title);
  90. btn_right=newButton(context);
  91. btn_right.setVisibility(View.INVISIBLE);//设置设置不可见
  92. btn_right.setBackgroundResource(R.drawable.buttonbg);//设置背景
  93. btn_right.setTextColor(Color.WHITE);//字体颜色
  94. if(right_drawable!=0){
  95. btn_right.setBackgroundResource(right_drawable);
  96. }else{
  97. btn_right.setBackgroundResource(R.drawable.backbg);//设置背景
  98. }
  99. if(null!=strBtnRight){
  100. LayoutParamsbtnRightParams=newLayoutParams(
  101. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  102. btnRightParams.setMargins(0,0,10,0);
  103. btn_right.setLayoutParams(btnRightParams);
  104. btn_right.setText(strBtnRight);
  105. btn_right.setVisibility(View.VISIBLE);
  106. }else{
  107. btn_right.setLayoutParams(newLayoutParams(50,50));
  108. }
  109. //添加这个按钮
  110. addView(btn_right);
  111. }
  112. publicButtongetBtn_left(){
  113. returnbtn_left;
  114. }
  115. publicButtongetBtn_right(){
  116. returnbtn_right;
  117. }
  118. publicTextViewgetTv_title(){
  119. returntv_title;
  120. }
  121. publicStringgetStrBtnLeft(){
  122. returnstrBtnLeft;
  123. }
  124. publicvoidsetStrBtnLeft(StringstrBtnLeft){
  125. this.strBtnLeft=strBtnLeft;
  126. }
  127. publicStringgetStrBtnRight(){
  128. returnstrBtnRight;
  129. }
  130. publicvoidsetStrBtnRight(StringstrBtnRight){
  131. this.strBtnRight=strBtnRight;
  132. }
  133. publicStringgetStrTitle(){
  134. returnstrTitle;
  135. }
  136. publicvoidsetStrTitle(StringstrTitle){
  137. this.strTitle=strTitle;
  138. }
  139. publicintgetLeft_drawable(){
  140. returnleft_drawable;
  141. }
  142. publicvoidsetLeft_drawable(intleft_drawable){
  143. this.left_drawable=left_drawable;
  144. }
  145. publicintgetRight_drawable(){
  146. returnright_drawable;
  147. }
  148. publicvoidsetRight_drawable(intright_drawable){
  149. this.right_drawable=right_drawable;
  150. }
  151. }

如何去引用或者使用我们的那个新添加的属性呢?首先要引入那个命名空间[我这是引用C#使用自定义控件的说法,暂时也通俗的这样理解吧]:

xmlns:navigation="http://schemas.android.com/apk/res/com.jiahui.titlebar"

然后再使用我们自定义的属性:navigation:btn_rightText,navigation:btn_leftText等等.

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:navigation="http://schemas.android.com/apk/res/com.jiahui.titlebar"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical">
  7. <com.jiahui.titlebar.UINavigationView
  8. android:id="@+id/uinavigationView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="45dip"
  11. navigation:btn_leftText="@string/back"
  12. navigation:btn_rightText="@string/done"
  13. navigation:left_drawable="@drawable/backbg"
  14. navigation:right_drawable="@drawable/buttonbg"
  15. navigation:tv_title="MyApplication"/>
  16. </LinearLayout>

好了,以上就是我要和大家分享的这点东西,有什么不懂的,可以看代码里的注释,代码基本上全贴上了,还有需要代码参考的可以留下方式.欢迎加入Android菜鸟联盟:252773976

如需转载引用请注明出处:http://blog.csdn.net/jiahui524

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值