自定义控件属性

来源:互联网 发布:王心凌怎么消失了知乎 编辑:程序博客网 时间:2024/06/05 16:46
demo:defineView
1.如何自定义控件属性?
2.如何动态创建组件?
3.接口回调思想

设计需要的属性
values新建attrs.xml。通过<declare-styleable>来告诉系统这是自定义的属性
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name ="Topbar">
  4. <attr name = "title" format="string"/>
  5. <attr name = "titleSize" format = "dimension"/>
  6. <attr name = "titleTextColor" format = "color"/>
  7. <attr name = "leftTextColor" format="color"/>
  8. <attr name = "leftBackground" format = "reference|color"/>
  9. <attr name = "leftText" format = "string"/>
  10. <attr name = "rightTextColor" format="color"/>
  11. <attr name = "rightBackground" format = "reference|color"/>
  12. <attr name = "rightText" format = "string"/>
  13. </declare-styleable>
  14. </resources>
实现一个自定义的“View”
重写构造方法,在构造方法中取出自定义值存到TapeArray中,从TypeArray中取出相应的值给了相应的变量,然后组合控件并添加到
ViewGroup中,所有布局属性都在里面设置。
  1. public class Topbar extends RelativeLayout {
  2. private Button leftButton,rightButton;
  3. private TextView tvText;
  4. /**
  5. * 左边字体的颜色
  6. */
  7. private int leftTextColor;
  8. /**
  9. * 左边背景的颜色
  10. */
  11. private Drawable leftBackground;
  12. /**
  13. * 左边butto文字
  14. */
  15. private String leftText;
  16. /**
  17. * 右边字体的颜色
  18. */
  19. private int rightTextColor;
  20. /**
  21. * 右边背景的颜色
  22. */
  23. private Drawable rightBackground;
  24. /**
  25. * 右边butto文字
  26. */
  27. private String rightText;
  28. /**
  29. * title要显示文字的大小
  30. */
  31. private float titleTextSize;
  32. /**
  33. * title字体的颜色
  34. */
  35. private int titleTextColor;
  36. /**
  37. * title的文字
  38. */
  39. private String title;
  40. private LayoutParams leftParams,rightParams,titleParams;
  41. private topbarClickListener listener;
  42. /**
  43. * 实现自己的接口
  44. * @author Kevin
  45. *
  46. */
  47. public interface topbarClickListener{
  48. public void leftClick();
  49. public void rightClick();
  50. }
  51. /**
  52. * 将传进来的映射
  53. * @param listener
  54. */
  55. public void setOnTopClickListener(topbarClickListener listener){
  56. this.listener = listener;
  57. }
  58. //需要自定义属性,所以需要attrs参数
  59. public Topbar(Context context, AttributeSet attrs) {
  60. super(context, attrs);
  61. //存取自定义属性中的值返回到TypedArray
  62. TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);
  63. //完成属性赋值
  64. leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
  65. leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
  66. rightText = ta.getString(R.styleable.Topbar_rightText);
  67. rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
  68. rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
  69. leftText = ta.getString(R.styleable.Topbar_leftText);
  70. titleTextSize = ta.getDimension(R.styleable.Topbar_titleSize,0);
  71. titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);
  72. title = ta.getString(R.styleable.Topbar_title);
  73. //TypedArray一定要回收,避免浪费资源和由于缓存造成的错误
  74. ta.recycle();
  75. leftButton = new Button(context);
  76. rightButton = new Button(context);
  77. tvText = new TextView(context);
  78. //自定义属性赋值给自定义的控件
  79. leftButton.setTextColor(leftTextColor);
  80. leftButton.setBackgroundDrawable(leftBackground);
  81. leftButton.setText(leftText);
  82. rightButton.setTextColor(rightTextColor);
  83. rightButton.setBackgroundDrawable(rightBackground);
  84. rightButton.setText(rightText);
  85. tvText.setTextColor(titleTextColor);
  86. tvText.setTextSize(titleTextSize);
  87. tvText.setText(title);
  88. tvText.setGravity(Gravity.CENTER);
  89. setBackgroundColor(0xFFF59563);
  90. //设置布局参数
  91. leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  92. //居左对齐
  93. leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
  94. //以leftParams的方式添加进去
  95. addView(leftButton,leftParams);
  96. //设置布局参数
  97. rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  98. //居右对齐
  99. rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
  100. addView(rightButton,rightParams);
  101. titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
  102. titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
  103. addView(tvText,titleParams);
  104. leftButton.setOnClickListener(new OnClickListener() {
  105. @Override
  106. public void onClick(View v) {
  107. listener.leftClick();
  108. }
  109. });
  110. rightButton.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. listener.rightClick();
  114. }
  115. });
  116. }
  117. }
接口回调抽象了使用的方法,点击事件抽象了出来,让调用者决定到底做什么,topbar就被封装起来了。
引用“View”
xmlns是命名空间,需要添加命名空间来引用自己的属性,AS中auto就行,Eclipse需要完整的包名。
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:custom="http://schemas.android.com/apk/res/com.llc.defineview"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.llc.defineview.MainActivity" >
  7. <com.llc.defineview.Topbar
  8. android:id="@+id/topbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="40dp"
  11. custom:leftBackground="@drawable/back_bg"
  12. custom:leftText="back"
  13. custom:leftTextColor="#ffffff"
  14. custom:rightBackground="@drawable/edit_bg"
  15. custom:rightText="more"
  16. custom:rightTextColor="#ffffff"
  17. custom:title="自定义标题"
  18. custom:titleTextColor="#123421"
  19. custom:titleSize="10sp">
  20. </com.llc.defineview.Topbar>
  21. </RelativeLayout>
MainActivity.java
  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Topbar topbar = (Topbar) findViewById(R.id.topbar);
  7. topbar.setOnTopClickListener(new Topbar.topbarClickListener() {
  8. @Override
  9. public void rightClick() {
  10. Toast.makeText(MainActivity.this, "left", Toast.LENGTH_LONG).show();
  11. }
  12. @Override
  13. public void leftClick() {
  14. Toast.makeText(MainActivity.this, "right", Toast.LENGTH_LONG).show();
  15. }
  16. });
  17. }
  18. }





来自为知笔记(Wiz)


0 0
原创粉丝点击