亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中自定義一個View的方法詳解

 更新時間:2016年07月27日 16:20:10   作者:feelang  
這篇文章主要介紹了Android中自定義一個View的方法,結(jié)合實例形式較為詳細的分析了Android中自定義View的具體步驟與相關(guān)注意事項,需要的朋友可以參考下

本文實例講述了Android中自定義一個View的方法。分享給大家供大家參考,具體如下:

Android中自定義View的實現(xiàn)比較簡單,無非就是繼承父類,然后重載方法,即便如此,在實際編碼中難免會遇到一些坑,我把自己遇到的一些問題和解決方法總結(jié)一下,希望對廣大碼友們有所幫助。

注意點① 用xml定義Layout時,Root element 最好使用merge

當我們需要繼承一個布局比較復雜的ViewGroup(比較多的是LinearLayout、RelativeLayout)時,通常會用xml來寫布局,然后在自定義的View類中inflate這個定義了layout的xml文件。

首先新建一個名為 MyLayout 的 class 文件,在 init 方法中解析稍后定義的xml文件。

/**
 * Created by liangfei on 4/14/15.
 */
public class MyLayout extends LinearLayout {
  public MyLayout(Context context) {
    super(context);
    init();
  }
  private void init() {
    setOrientation(VERTICAL);
    View rootView = inflate(getContext(), R.layout.my_layout, this);
    ((TextView) rootView.findViewById(R.id.title)).setText("MyLayout");
    ((TextView) rootView.findViewById(R.id.desc)).setText("A customized layout");
  }
}

然后新建一個取名為my_layout的布局文件, 并把 Root element 設(shè)置成merge。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
  <TextView
    android:id="@+id/title"
    android:textSize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</merge>

用 Android SDK 附帶的 Monitor 工具查看一下運行時的布局信息。

最頂層是一個FrameLayout,然后是一個LinearLayout,里面有兩個TextView,可以看出布局沒有冗余。

但是,如果把 Root element 換成 LinearLayout,效果會怎么樣呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView
    android:id="@+id/title"
    android:textSize="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

很明顯,用 LinearLayout 做 Root element 后,布局多了一個層級,成了影響性能的一個因素。

注意點② 重載子類構(gòu)造函數(shù)時要弄清楚父類做了哪些操作

先從我一個慘痛的教訓開始,當時我這樣自定義了一個Button:

/**
 * Created by liangfei on 4/14/15.
 */
public class MyButton extends Button {
  public MyButton(Context context) {
    this(context, null);
  }
  public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }
}

乍一看貌似沒什么問題,構(gòu)造函數(shù)的調(diào)用方式都是正確的,但是無論我怎么修改 MyButton 的屬性,顯示方式就是不正確。

其實問題就出在Button類在構(gòu)造函數(shù)中使用了一個defStyleAttr, 而我這種寫法會忽略掉這個defStyleAttr - com.android.internal.R.attr.buttonStyle,稍讀源碼就知道了。

@RemoteView
public class Button extends TextView {
  public Button(Context context) {
    this(context, null);
  }
  public Button(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.buttonStyle);
  }
  public Button(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
  }
  public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }
}

后來寫代碼的時候,我都是看了父類的源碼之后才敢這么寫,如果不確定就老老實實地寫成下面這種形式。

/**
 * Created by liangfei on 4/14/15.
 */
public class MyButton extends Button {
  public MyButton(Context context) {
    super(context);
    init();
  }
  public MyButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }
}

其實,還有很多其他的坑,比如 Button 的高度,后面抽時間再總結(jié)一下

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • 基于Android實現(xiàn)可滾動的環(huán)形菜單效果

    基于Android實現(xiàn)可滾動的環(huán)形菜單效果

    這篇文章主要為大家詳細介紹了Android如何使用kotlin實現(xiàn)可滾動的環(huán)形菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MVVMLight項目的綁定及各種使用場景示例分析

    MVVMLight項目的綁定及各種使用場景示例分析

    這篇文章主要為大家介紹了MVVMLight項目中的綁定及綁定的各種使用場景示例源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步除夕快樂
    2022-01-01
  • Android實現(xiàn)計步器功能

    Android實現(xiàn)計步器功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)計步器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 詳解android 視頻圖片混合輪播實現(xiàn)

    詳解android 視頻圖片混合輪播實現(xiàn)

    這篇文章主要介紹了android 視頻圖片混合輪播實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • Android選項菜單用法實例分析

    Android選項菜單用法實例分析

    這篇文章主要介紹了Android選項菜單用法,以完整實例形式較為詳細分析了Android選項菜單的布局及功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Android仿支付寶笑臉刷新加載動畫的實現(xiàn)代碼

    Android仿支付寶笑臉刷新加載動畫的實現(xiàn)代碼

    這篇文章主要介紹了Android仿支付寶笑臉刷新加載動畫的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android記事本項目開發(fā)

    Android記事本項目開發(fā)

    這篇文章主要為大家詳細介紹了Android記事本項目開發(fā)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android?ScrollView實現(xiàn)滾動超過邊界松手回彈

    Android?ScrollView實現(xiàn)滾動超過邊界松手回彈

    這篇文章主要為大家詳細介紹了Android?ScrollView實現(xiàn)滾動超過邊界松手回彈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 一篇文章揭開Kotlin協(xié)程的神秘面紗

    一篇文章揭開Kotlin協(xié)程的神秘面紗

    最近看了下Kotlin的協(xié)程,覺得挺好的,寫篇文章總結(jié)總結(jié),所以下面這篇文章主要給大家介紹了關(guān)于Kotlin協(xié)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-08-08
  • Android自定義控件實現(xiàn)圓形進度CircleProgressBar

    Android自定義控件實現(xiàn)圓形進度CircleProgressBar

    這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)圓形進度CircleProgressBar,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評論