Android 學習到一點程度,想繼續深造學習就必然會開始接觸自定義控件,自定義控件能夠分紅3種類型,能夠說是3個難度,最簡單的是組合控件,其次是繼承View 自定義控件,最爲複雜的是繼承ViewGroup的自定義控件,今天咱們就重最簡單的自定義組合控件開始學習。android
自定義組合控件能夠分紅3個步驟,app
第一步 選擇繼承哪一個空間 View 仍是 ViewGroup ;ide
第二步 重寫3個構成方法,在valus 目錄下建立attrs 聲明一些根據業務需求的屬性佈局
第三步 顯示學習
須要注意:自定義控件須要應用命名空間和完整包名,具體看如下代碼ui
第一步 : 繼承了LinearLayout this
package com.example.CombinationView;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
/**
* Created by ADMIN on 2015/9/30.
*/
public class TopView extends LinearLayout {
int defaultViewType = 0;//默認的組合view
boolean left, right, section;
View leftView, rightView, sectionView;
public TopView(Context context) {
this(context, null);
}
// 對XML 的解析
public TopView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.Top);
defaultViewType = array.getInteger(R.styleable.Top_viewtype, 0);
left = array.getBoolean(R.styleable.Top_left, true);
right = array.getBoolean(R.styleable.Top_right, true);
section = array.getBoolean(R.styleable.Top_section, true);
array.recycle();
//
initView(context);
}
// 根據不一樣XML標記 更換不一樣的View
private void initView(Context context) {
View Viewtop;
switch (defaultViewType){
case 0:
Viewtop = TopView.inflate(context, R.layout.tob_view, this);
initChildrenView(Viewtop);
break;
case 1:
Viewtop = TopView.inflate(context, R.layout.tob_viewtwo, this);
initChildrenView(Viewtop);
break;
}
}
private void initChildrenView(View viewtop) {
leftView = viewtop.findViewById(R.id.left);
rightView =viewtop.findViewById(R.id.right);
sectionView = viewtop.findViewById(R.id.section);
leftView.setVisibility(left ? VISIBLE : GONE);
rightView.setVisibility(right ? VISIBLE : GONE);
sectionView.setVisibility(section ? VISIBLE : GONE);
}
/**
* 左邊點擊事件監聽
* @param onClickListener
*/
public void LeftViewClick(OnClickListener onClickListener){
leftView.setOnClickListener(onClickListener);
}
/**
* 右邊點擊事件監聽
* @param onClickListener
*/
public void RigthViewClick(OnClickListener onClickListener){
rightView.setOnClickListener(onClickListener);
}
public View getSectionView() {
return sectionView;
}
public View getLeftView() {
return leftView;
}
public View getRightView() {
return rightView;
}
/**
* 對自定義內部的點解事件進行所有監聽傳遞到到外面Actity
對外包提供事件處理的方法
* @param onClickListener
*/
public void TopClick(OnClickListener onClickListener){
LeftViewClick(onClickListener);
RigthViewClick(onClickListener);
}
}
===================================================================
valus 目錄下建立attrs 聲明一些根據業務需求的屬性
<resources>
<declare-styleable name="Top">
<attr name="viewtype" format="integer"/>
<attr name="left" format="boolean" />
<attr name="right" format="boolean" />
<attr name="section" format="boolean" />
</declare-styleable>
</resources>
===============================================
在佈局文件中引用組合控件
須要注意的是
http://schemas.android.com/apk/res/ + package包名
xmlns:top="http://schemas.android.com/apk/res/com.example.CombinationView"
=========================================
xmlns:top="http://schemas.android.com/apk/res/com.example.CombinationView"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:top="http://schemas.android.com/apk/res/com.example.CombinationView"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.CombinationView.TopView
android:id="@+id/topview"
top:left="true"
top:viewtype="0"
android:layout_width="match_parent"
android:layout_height="60dp"/>
</LinearLayout>
========================================================================
在主activity 中 處理組合控件的每一個點擊事件
package com.example.CombinationView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
TopView topView;
View left;
View Right;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
topView = (TopView) findViewById(R.id.topview);
left = topView.getLeftView();
Right = topView.getRightView();
topView.TopClick(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v ==left){
System.out.println("點擊了左邊");
}else if(v==Right){
System.out.println("點解了==Right");
}
}
});
}
}
OK 完美收官,就這樣完成一個自定義組合控件, 不懂請留言