自定义了一个Button按钮,效果如图,有自定义背景(可根据文字长短扩展),自定义按钮图片和文字背景图片扩展,参考http://www.7747.net/kf/201104/88131.html 这个文章,http://blog.csdn.net/greatstar/archive/2010/10/12/5936177.aspx 和这个文章。使用draw9patch时遇到不能启动,原因是Android 2.3少了一个包,swing-worker-1.2 下载放在android-sdk\tools\lib 下遍可以正常使用。

其他直接上代码

?[Copy to clipboard] Download zuiniuwang.java
        
  1. <spanstyle="font-size:14px">importandroid.content.Context;
  2. importandroid.view.ViewGroup;
  3. importandroid.widget.ImageView;
  4. importandroid.widget.LinearLayout;
  5. importandroid.widget.RelativeLayout;
  6. importandroid.widget.TextView;
  7. publicclassMyViewTestextendsRelativeLayout{
  8. privatefinalintWC=LinearLayout.LayoutParams.WRAP_CONTENT;
  9. privatefinalintFP=LinearLayout.LayoutParams.FILL_PARENT;
  10. privatefinalstaticStringTAG=MyViewTest.class.getSimpleName();
  11. /**
  12. *
  13. *@paramcontext
  14. *@parambackgroudPic背景图片的ID为空则为默认
  15. *@paramimagePic按钮前面的图片为空则为默认
  16. *@paramtext按钮的文本为空则为默认
  17. */
  18. publicMyViewTest(Contextcontext,IntegerbackgroudPic,IntegerimagePic,Stringtext){
  19. super(context);
  20. //TODOAuto-generatedconstructorstub
  21. if(backgroudPic==null)
  22. this.setBackgroundResource(R.drawable.my_music_button);
  23. else
  24. this.setBackgroundResource(backgroudPic);
  25. ImageViewimageView=newImageView(context);
  26. if(imagePic==null)
  27. imageView.setImageResource(R.drawable.style1_button_icon_normal);
  28. else
  29. imageView.setImageResource(imagePic);
  30. imageView.setId(1);
  31. RelativeLayout.LayoutParamslp1=newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  32. ViewGroup.LayoutParams.WRAP_CONTENT);
  33. lp1.addRule(RelativeLayout.RIGHT_OF,1);
  34. TextViewtextView=newTextView(context);
  35. if(text==null)
  36. textView.setText("mycustombutton");
  37. else
  38. textView.setText(text);
  39. this.addView(imageView);
  40. this.addView(textView,lp1);
  41. }
  42. }

其中R.drawable.my_music_button 是一个XML文件,定义了按钮的选中点击等事件发生时,背景图片的变化,直接更改文件中相应的图片便可

?[Copy to clipboard] Download zuiniuwang.xml

        
  1. <spanstyle="font-size:14px"><?xmlversion="1.0"encoding="UTF-8"?>
  2. </span>
  3. <selectorxmlns:android="http://schemas.android.com/apk/res/android"><spanstyle="font-size:14px">
  4. </span><itemandroid:drawable="@drawable/my_button_bg"android:state_pressed="true"><spanstyle="font-size:14px">
  5. </span><itemandroid:drawable="@drawable/my_button_bg"android:state_focused="true"><spanstyle="font-size:14px">
  6. </span><itemandroid:drawable="@drawable/my_music_button_bg"><spanstyle="font-size:14px">
  7. </span></item></item></item></selector><spanstyle="font-size:14px">
  8. </span>

本文出自 “最牛傻蛋的博客” 博客,转载请与作者联系!

更多相关文章

  1. android 输入法弹出键盘把listview顶上去,保留顶部标题栏位置不动
  2. 系出名门Android(5) - 控件(View)之TextView, Button, ImageButt
  3. 关于visibility的属性值visible,invisible,gone的区别
  4. android的selector,背景选择器
  5. Android(安卓)关于RatingBar评分条
  6. android 单选按钮组的使用
  7. android 对话框AlertDialog
  8. android:动态创建多个按钮 并给每个按键添加监听事件
  9. Android_RadioGroup实现Tab

随机推荐

  1. Kotlin初体验
  2. Android(安卓)环境搭建
  3. Android(安卓)开发四大天王 四大组件
  4. Android下的POS打印机调用
  5. Android监听键盘显示和隐藏
  6. Android(安卓)studio 更新android SDK
  7. Android面试知识点1
  8. Android(安卓)NDK初识
  9. Android中的Intent标准跳转应用
  10. Suggestion: add 'tools:replace="androi