android自定义View——组合控件

川长思鸟来 2022-06-08 01:51 422阅读 0赞

一,view_setting_item.xml组合控件布局文件:这里是组合控件中的元素,一个TextView,一个EditText.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
  3. <EditText android:id="@+id/et_setting_item" android:layout_width="match_parent" android:layout_height="48dp" android:background="@android:color/transparent"//设置输入框背景透明 android:gravity="center"/>
  4. <TextView android:id="@+id/tv_setting_item" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:layout_marginLeft="10dp"/>
  5. </FrameLayout>

二,attrs.xml文件:定义组合控件的自定义属性

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="SettingItemView">
  4. <attr name="title_text" format="string"/>//TextView要显示的字符串
  5. <attr name="title_color" format="color"/>//TextView文字颜色
  6. <attr name="title_size" format="dimension"/>//TextView字符大小
  7. <attr name="title_hide" format="boolean"/>//TextView是否隐藏
  8. <attr name="content_text" format="string"/>//EditText要显示的字符串
  9. <attr name="content_size" format="dimension"/>//EditText字符大小
  10. <attr name="content_color" format="color"/>//EditText文字颜色
  11. <attr name="content_hint_color" format="color"/>//EditText提示文字颜色
  12. <attr name="content_focus" format="boolean"/>//EditText是否可以编辑
  13. </declare-styleable>
  14. </resources>

三,SettingItemView.java组合控件类

  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.graphics.Color;
  4. import android.util.AttributeSet;
  5. import android.util.TypedValue;
  6. import android.view.LayoutInflater;
  7. import android.widget.EditText;
  8. import android.widget.FrameLayout;
  9. import android.widget.TextView;
  10. import com.sdlj.vehiclerepair.activity.R;
  11. /** * Created by Chunna.zheng on 2017/9/9. */
  12. public class SettingItemView extends FrameLayout{
  13. private EditText etMsg;
  14. private TextView tvTitle;
  15. private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
  16. private static final int DEFAULT_EDIT_COLOR = Color.TRANSPARENT;
  17. private String titleText;
  18. private float titleSize;
  19. private int titleColor;
  20. private boolean isTitleHide;
  21. private float contentSize;
  22. private int contentColor;
  23. private int contentHintColor;
  24. private String contentText;
  25. private boolean isContentFocus;
  26. private int DEFAULT_TITLE_SIZE = 16;
  27. private int DEFAULT_TITLE_COLOR = Color.GRAY;
  28. private boolean DEFAULT_TITLE_HIDE = false;
  29. private int DEFAULT_CONTENT_SIZE = 16;
  30. private int DEFAULT_CONTENT_COLOR = Color.BLACK;
  31. private int DEFAULT_CONTENT_HINT_COLOR = Color.GRAY;
  32. private boolean DEFAULT_CONTENT_FOCUS = true;
  33. public SettingItemView(Context context, AttributeSet attrs) {
  34. super(context, attrs);
  35. //加载属性值
  36. initAttrs(context, attrs);
  37. //加载布局
  38. initView(context);
  39. }
  40. private void initAttrs(Context context, AttributeSet attrs) {
  41. TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SettingItemView);
  42. titleText = typedArray.getString(R.styleable.SettingItemView_title_text);
  43. titleSize = typedArray.getDimension(R.styleable.SettingItemView_title_size, DEFAULT_TITLE_SIZE);
  44. titleColor = typedArray.getColor(R.styleable.SettingItemView_title_color, DEFAULT_TITLE_COLOR);
  45. isTitleHide = typedArray.getBoolean(R.styleable.SettingItemView_title_hide, DEFAULT_TITLE_HIDE);
  46. contentText = typedArray.getString(R.styleable.SettingItemView_content_text);
  47. contentSize = typedArray.getDimension(R.styleable.SettingItemView_content_size, DEFAULT_CONTENT_SIZE);
  48. contentColor = typedArray.getColor(R.styleable.SettingItemView_content_color, DEFAULT_CONTENT_COLOR);
  49. contentHintColor = typedArray.getColor(R.styleable.SettingItemView_content_hint_color, DEFAULT_CONTENT_HINT_COLOR);
  50. isContentFocus = typedArray.getBoolean(R.styleable.SettingItemView_content_focus, DEFAULT_CONTENT_FOCUS);
  51. typedArray.recycle();
  52. }
  53. private void initView(Context context) {
  54. LayoutInflater.from(context).inflate(R.layout.view_setting_item, this);// 加载布局
  55. etMsg = (EditText) findViewById(R.id.et_setting_item);// 获取控件
  56. tvTitle = (TextView) findViewById(R.id.tv_setting_item);
  57. tvTitle.setText(titleText);
  58. tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
  59. //如果不设置这个文字会变得非常大
  60. tvTitle.getPaint().setTextSize(titleSize);
  61. tvTitle.setTextColor(titleColor);
  62. if(isTitleHide){
  63. tvTitle.setVisibility(GONE);
  64. }
  65. etMsg.setText(contentText);
  66. etMsg.setTextSize(TypedValue.COMPLEX_UNIT_SP,contentSize);
  67. //如果不设置这个文字会变得非常大
  68. etMsg.getPaint().setTextSize(contentSize);
  69. etMsg.setTextColor(contentColor);
  70. etMsg.setHintTextColor(contentHintColor);
  71. if(!isContentFocus){
  72. etMsg.setFocusable(false);
  73. }
  74. }
  75. //设置EditText值
  76. public void setContentText(String str){
  77. etMsg.setText(str);
  78. }
  79. //获取EditText值
  80. public String getContentText(){
  81. return etMsg.getText().toString().trim();
  82. }
  83. }

四,组合控件使用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"//自定义属性使用声明,必须要写 tools:context="com.sdlj.vehiclerepair.activity.SettiingActivity">
  3. <com.sdlj.vehiclerepair.view.SettingItemView android:layout_width="match_parent" android:layout_height="48dp" app:title_text="头像" app:title_color = "#000000" app:title_size = "16sp" app:content_size = "16sp" app:content_focus="false"/>
  4. </LinearLayout>

发表评论

表情:
评论列表 (有 0 条评论,422人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义View(一)

    1.自定义控件View的介绍   很多android入门的新手对于自定义控件,心里估计会有一种恐惧的心理把,起初我也是这样的,不过不用怕,因为如果你要进一步的进阶称为高手