① android的自定義View的實現原理哪位能給我個思路呢。謝謝。
如果說要按類型來劃分的話,自定義View的實現方式大概可以分為三種,自繪控制項、組合控制項、以及繼承控制項。那麼下面我們就來依次學習一下,每種方式分別是如何自定義View的。
一、自繪控制項
自繪控制項的意思就是,這個View上所展現的內容全部都是我們自己繪制出來的。繪制的代碼是寫在onDraw()方法中的,而這部分內容我們已經在Android視圖繪制流程完全解析,帶你一步步深入了解View(二)中學習過了。
下面我們准備來自定義一個計數器View,這個View可以響應用戶的點擊事件,並自動記錄一共點擊了多少次。新建一個CounterView繼承自View,代碼如下所示:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffcb05">
<Button
android:id="@+id/button_left"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@drawable/back_button"
android:text="Back"
android:textColor="#fff"/>
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ThisisTitle"
android:textColor="#fff"
android:textSize="20sp"/>
</RelativeLayout>
在這個布局文件中,我們首先定義了一個RelativeLayout作為背景布局,然後在這個布局裡定義了一個Button和一個TextView,Button就是標題欄中的返回按鈕,TextView就是標題欄中的顯示的文字。
接下來創建一個TitleView繼承自FrameLayout,代碼如下所示:
{
privateButtonleftButton;
privateTextViewtitleText;
publicTitleView(Contextcontext,AttributeSetattrs){
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
titleText=(TextView)findViewById(R.id.title_text);
leftButton=(Button)findViewById(R.id.button_left);
leftButton.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
((Activity)getContext()).finish();
}
});
}
publicvoidsetTitleText(Stringtext){
titleText.setText(text);
}
publicvoidsetLeftButtonText(Stringtext){
leftButton.setText(text);
}
(OnClickListenerl){
leftButton.setOnClickListener(l);
}
}
TitleView中的代碼非常簡單,在TitleView的構建方法中,我們調用了LayoutInflater的inflate()方法來載入剛剛定義的title.xml布局,這部分內容我們已經在Android LayoutInflater原理分析,帶你一步步深入了解View(一)這篇文章中學習過了。
接下來調用findViewById()方法獲取到了返回按鈕的實例,然後在它的onClick事件中調用finish()方法來關閉當前的Activity,也就相當於實現返回功能了。
另外,為了讓TitleView有更強地擴展性,我們還提供了setTitleText()、setLeftButtonText()、setLeftButtonListener()等方法,分別用於設置標題欄上的文字、返回按鈕上的文字、以及返回按鈕的點擊事件。
到了這里,一個自定義的標題欄就完成了,那麼下面又到了如何引用這個自定義View的部分,其實方法基本都是相同的,在布局文件中添加如下代碼:
<RelativeLayoutxmlns: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">
<com.example.customview.TitleView
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.customview.TitleView>
</RelativeLayout>
這樣就成功將一個標題欄控制項引入到布局文件中了,運行一下程序。
現在點擊一下Back按鈕,就可以關閉當前的Activity了。如果你想要修改標題欄上顯示的內容,或者返回按鈕的默認事件,只需要在Activity中通過findViewById()方法得到TitleView的實例,然後調用setTitleText()、setLeftButtonText()、setLeftButtonListener()等方法進行設置就OK了。
② Android 自定義View:為什麼你設置的wrap_content不起作用
在使用自定義View時,View寬 / 高的 wrap_content 屬性不起自身應有的作用,而且是起到與 match_parent 相同作用。
其實這里有兩個問題:
請分析 & 解決問題之前,請先看自定義View原理中 (2)自定義View Measure過程 - 最易懂的自定義View原理系列
問題出現在View的寬 / 高設置,那我們直接來看自定義View繪制中第一步對View寬 / 高設置的過程:measure過程中的 onMeasure() 方法
繼續往下看 getDefaultSize()
從上面發現:
那麼有人會問:wrap_content和match_parent具有相同的效果,為什麼是填充父容器的效果呢?
我們知道,子View的MeasureSpec值是根據子View的布局參數(LayoutParams)和父容器的MeasureSpec值計算得來,具體計算邏輯封裝在getChildMeasureSpec()里。
接下來,我們看生成子View MeasureSpec的方法: getChildMeasureSpec() 的源碼分析:
getChildMeasureSpec()
從上面可以看出,當子View的布局參數使用 match_parent 或 wrap_content 時:
所以: wrap_content 起到了和 match_parent 相同的作用:等於父容器當前剩餘空間大小
當自定義View的布局參數設置成wrap_content時時,指定一個默認大小(寬 / 高)。
這樣,當你的自定義View的寬 / 高設置成wrap_content屬性時就會生效了。
網上流傳著這么一個解決方案:
答: 是,當父View為 AT_MOST 、View為 match_parent 時,該View的 match_parent 的效果就等於 wrap_content 。上述方法存在邏輯錯誤,但由於這種情況非常特殊的,所以導致最終的結果沒有錯誤。具體分析請看下面例子:
從上面的效果可以看出,View大小 = 默認值
我再將子View的屬性改為 wrap_content :
從上面的效果可以看出,View大小還是等於默認值。
相信看到這里你已經看懂了:
為了更好的表示判斷邏輯,我建議你們用本文提供的解決方案,即根據布局參數判斷默認值的設置
不定期分享關於 安卓開發 的干貨,追求 短、平、快 ,但 卻不缺深度 。