網站首頁 編程語言 正文
1.自定義View簡介
自定義view可以被認為是繼承自View,系統沒有的效果(ImageView,TextView,Button),extents View,extents ViewGrop
2.構造方法
繼承View。View有四個構造方法,下面講述四個構造方法什么時候調用:
第一個構造方法會在代碼中new的時候調用
TextView textView = new TextView(this);
public TextView(Context context) { super(context); }
第二個構造方法在布局layout中使用(調用)
<com.zrc.view_java_demo_01.TextView android:layout_width="match_parent" android:layout_height="match_parent"/>
public TextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }
第三個構造方法在布局layout中使用(調用),但是會有style
調用 <com.zrc.view_java_demo_01.TextView style="@style/defualt"/>
<style name="defualt" > <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">@color/colorAccent</item> </style>
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
還有第四個構造方法,在用到時候,再做說明,在這里就不再展開。
3.onMeasure()
獲取寬高的模式
int widthSize = MeasureSpec.getMode(widthMeasureSpec); //獲取前兩位 int heightSize = MeasureSpec.getMode(heightMeasureSpec);
獲取寬高的值,指定控件的寬高
int widthSize = MeasureSpec.getSize(widthMeasureSpec); //獲取后面30位 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
MeasureSpec.AT_MOST:在布局中指定了wrap_content
MeasureSpec.EXACTLY: 在布局中特定的值 100dp match_parent fill_parent
MeasureSpec.UNSPECIFIED:盡可能的大,很少用到。listview,Scrollview 在測量子布局時候會用UNSPECIFIED
Scrollview+ListView會出現顯示不全的現象?
widthMeasureSpec widthMeasureSpec : 會包含兩個信息是一個32位的值,第一個信息是模式:2位 值:30位
4.onDraw()
/** * 用于繪制 * */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //畫文本 canvas.drawText(); //畫弧 canvas.drawArc(); //畫圓 canvas.drawCircle(); }
5.onTouch()
/** * 處理用戶交互的,手指觸摸等等(事件分發事件攔截) * */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //手指按下 Log.e("TAG","手指按下"); break; case MotionEvent.ACTION_MOVE: //手指移動 Log.e("TAG","手指移動"); break; case MotionEvent.ACTION_UP: //手指抬起 Log.e("TAG","手指抬起"); break; } return super.onTouchEvent(event); }
6.自定義屬性
自定義屬性就是用來配置的,android:text = "Darren"是系統自定義屬性
6.1在res下的values下面新建attrs.xml
<!-- name 屬性名稱 format 格式: string 文字 color:顏色 dimension 寬高 字體大小 integer 數字 reference 資源(drawable) --> <attr name="text" format="string"/> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> <attr name="maxLength" format="integer"/> <attr name="background" format="reference|color"/> <!-- 枚舉 --> <attr name="inputType"> <enum name="number" value="1"/> <enum name="text" value="2"/> <enum name="password" value="3"/> </attr> </declare-styleable>
6.2在布局中使用
聲明命名空間,然后在自己的自定義View中使用
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.zrc.view_java_demo_01.TextView app:text="Darren" app:textColor="@color/colorAccent" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
6.3在自定義View中獲取屬性
// 獲取自定義屬性 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView); mText = array.getString(R.styleable.TextView_text); mTextColor = array.getColor(R.styleable.TextView_textColor,mTextColor); mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize); // 回收 array.recycle();
原文鏈接:https://blog.csdn.net/weixin_43912367/article/details/105643064
相關推薦
- 2022-04-18 Python的類成員變量默認初始值的坑及解決_python
- 2022-03-14 1709 - Index column size too large. The maximum co
- 2022-08-05 Python?如何給圖像分類(圖像識別模型構建)_python
- 2022-08-25 C語言詳細分析結構體的內存對齊規則_C 語言
- 2022-09-28 C語言關于二叉樹中堆的創建和使用整理_C 語言
- 2023-03-22 tkinter如何實現打開文件對話框并獲取文件絕對路徑_python
- 2022-08-26 .net任務調度框架FluentScheduler簡介_實用技巧
- 2022-06-20 C語言手把手帶你掌握帶頭雙向循環鏈表_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支