最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中自定義一個(gè)View的方法詳解

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

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

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

注意點(diǎn)① 用xml定義Layout時(shí),Root element 最好使用merge

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

首先新建一個(gè)名為 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");
  }
}

然后新建一個(gè)取名為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 工具查看一下運(yùn)行時(shí)的布局信息。

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

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

<?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è)層級(jí),成了影響性能的一個(gè)因素。

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

先從我一個(gè)慘痛的教訓(xùn)開始,當(dāng)時(shí)我這樣自定義了一個(gè)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 的屬性,顯示方式就是不正確。

其實(shí)問題就出在Button類在構(gòu)造函數(shù)中使用了一個(gè)defStyleAttr, 而我這種寫法會(huì)忽略掉這個(gè)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);
  }
}

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

/**
 * 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();
  }
}

其實(shí),還有很多其他的坑,比如 Button 的高度,后面抽時(shí)間再總結(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ā)入門與進(jìn)階教程》及《Android控件用法總結(jié)

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

相關(guān)文章

  • Android實(shí)現(xiàn)可復(fù)用的篩選頁面

    Android實(shí)現(xiàn)可復(fù)用的篩選頁面

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可復(fù)用的篩選頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Kotlin中協(xié)變、逆變和不變示例詳解

    Kotlin中協(xié)變、逆變和不變示例詳解

    這篇文章主要給大家介紹了關(guān)于Kotlin中協(xié)變、逆變和不變的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • android實(shí)現(xiàn)左右側(cè)滑菜單效果

    android實(shí)現(xiàn)左右側(cè)滑菜單效果

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)左右側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • android開發(fā)教程之自定義屬性用法詳解

    android開發(fā)教程之自定義屬性用法詳解

    這篇文章主要介紹了android開發(fā)中的自定義屬性用法詳解,需要的朋友可以參考下
    2014-04-04
  • 關(guān)于Android中自定義ClassLoader耗時(shí)問題的追查

    關(guān)于Android中自定義ClassLoader耗時(shí)問題的追查

    熱修復(fù)和插件化是目前比較熱門的技術(shù),要想更好的掌握它們需要了解ClassLoader,下面這篇文章主要給大家介紹了關(guān)于Android中自定義ClassLoader耗時(shí)問題追查的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-06-06
  • Kotlin協(xié)程基礎(chǔ)元素梳理分析

    Kotlin協(xié)程基礎(chǔ)元素梳理分析

    這篇文章我們來講協(xié)程的基礎(chǔ)元素,畢竟協(xié)程是一個(gè)很強(qiáng)大的設(shè)計(jì)模式,深入了解需要花很多的時(shí)間,我們先從簡(jiǎn)單開始,其實(shí)學(xué)會(huì)了簡(jiǎn)單的使用,基本已經(jīng)可以滿足我們平時(shí)的開發(fā)需要了,話不多說,開始
    2022-11-11
  • Android RecyclerView添加上拉加載更多效果

    Android RecyclerView添加上拉加載更多效果

    這篇文章主要為大家詳細(xì)介紹了Android RecyclerView添加上拉加載更多效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • android 電話狀態(tài)監(jiān)聽(來電和去電)實(shí)現(xiàn)代碼

    android 電話狀態(tài)監(jiān)聽(來電和去電)實(shí)現(xiàn)代碼

    從事android開發(fā)的朋友們可能電話狀態(tài)監(jiān)聽不是很擅長(zhǎng),接下來將詳細(xì)介紹電話狀態(tài)監(jiān)聽功能的實(shí)現(xiàn)步驟,需要了解的朋友可以參考下
    2012-12-12
  • Android中使用Spinner實(shí)現(xiàn)下拉列表功能

    Android中使用Spinner實(shí)現(xiàn)下拉列表功能

    Spinner是一個(gè)列表選擇框,會(huì)在用戶選擇后,展示一個(gè)列表供用戶進(jìn)行選擇。下面通過本文給大家實(shí)例詳解android中使用Spinner實(shí)現(xiàn)下拉列表功能,一起看看吧
    2017-04-04
  • Android中Edittext設(shè)置輸入條件

    Android中Edittext設(shè)置輸入條件

    本篇文章主要介紹了Android中Edittext設(shè)置輸入條件的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-05-05

最新評(píng)論

康保县| 云浮市| 永嘉县| 汉阴县| 泽州县| 大连市| 儋州市| 乐清市| 清水县| 海门市| 东丰县| 安多县| 溆浦县| 建阳市| 山东| 永康市| 出国| 康平县| 永善县| 金山区| 华容县| 东丽区| 崇明县| 舞阳县| 罗定市| 岳池县| 贡嘎县| 罗平县| 报价| 东至县| 廊坊市| 巫山县| 舒城县| 宝丰县| 成武县| 上栗县| 阳泉市| 体育| 民和| 什邡市| 华安县|