您的位置:首页 > 移动开发 > Android开发

Android自定义控件之组合控件

2015-09-30 12:55 429 查看
Android 学习到一点程度,想继续深造学习就必然会开始接触自定义控件,自定义控件可以分成3种类型,可以说是3个难度,最简单的是组合控件,其次是继承View 自定义控件,最为复杂的是继承ViewGroup的自定义控件,今天我们就重最简单的自定义组合控件开始学习。

自定义组合控件可以分成3个步骤,

第一步 选择继承哪个空间 View 还是 ViewGroup ;

第二步 重写3个构成方法,在valus 目录下创建attrs 声明一些根据业务需求的属性

第三步 显示

需要注意:自定义控件需要应用命名空间和完整包名,具体看以下代码

第一步 : 继承了LinearLayout

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 完美收官,就这样完成一个自定义组合控件, 不懂请留言
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: