今晚是几分焦虑的学习呀,想着第一次面试在周六,想多学点知识,但是发现好像其实自己的基础都学完了,在找多点进阶的学,离题了,恩,好的,入正题,如何在安卓中使用自定义View控件,并且实现动态的实现,接口回调机制等。
这篇文章仅仅是组合控件,不涉及自定义View的绘制。开始!!!
1、首先在values下新建一个attrs的xml文件,指定自定义控件的各种属性
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定义控件 -->
<declare-styleable name="TopBar">
<!-- 自定义title的三个属性 -->
<attr name="Title" format="string" />
<attr name="TitleColor" format="color" />
<attr name="TitleSize" format="dimension" />
<!-- 自定义Button的三个属性 -->
<attr name="ButtonTitle" format="string" />
<attr name="ButtonColor" format="color" />
<attr name="ButtonSize" format="dimension" />
</declare-styleable>
</resources>
2、自定义一个View的Java文件,这个文件是用来实例化attrs里面的各种属性的
步骤:
1、初始化界面的控件
//1、自定义控件初始化
private Button button;
private TextView title;
2、初始化attrs的各种属性,将attrs里面的各种属性放在TypedArray中(TypedArray类似于一个Map集合,可以通过key取得value)
//2、自定义属性的初始化
//title的属性
private String Title;
private int TitleColor;
private float TitleSize;
//Button的属性
private String ButtonTitle;
private int ButtonColor;
private float ButtonSize;
3、将存放在TypedArray中的属性取出,设置给我们初始化的属性,同时将我们的属性设置给自定义控件
//2、将存储在TypedArray中的属性添加到并回收TypedArray
Title=ta.getString(R.styleable.TopBar_Title);
TitleColor=ta.getColor(R.styleable.TopBar_TitleColor, 0);
TitleSize=ta.getFloat(R.styleable.TopBar_TitleSize, 16);
ButtonTitle=ta.getString(R.styleable.TopBar_ButtonTitle);
ButtonColor=ta.getColor(R.styleable.TopBar_ButtonColor, 0);
ButtonSize=ta.getFloat(R.styleable.TopBar_ButtonSize, 14);
ta.recycle();
//3、将各自的属性增加到各自的空间中
button=new Button(context);
title=new TextView(context);
title.setText(Title);
title.setTextColor(TitleColor);
title.setTextSize(TitleSize);
title.setGravity(Gravity.CENTER);
button.setText(ButtonTitle);
button.setTextColor(ButtonColor);
button.setTextSize(ButtonSize);
//到这里已经完成了自定义控件的各个组成成分
setBackgroundColor(0xFF00FFDA);
4、当预定义在attrs中的属性关联控件之后,剩下的是设置控件位于View中位置,可以使用LayoutParams进行设置
//首先使用 LayoutParams进行剩余属性的设置
TitleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
ButtonParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
ButtonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
5、使用ViewGroup的addView()方法将我们的自定义控件添加到自定义View中,静态的自定义View就基本实现了
addView(title, TitleParams);
addView(button, ButtonParams);
6、实现View的动态变换,我们使用类似于View的点击事件的接口回调机制进行实现
接口回调机制:是一种作为模板暴露出去的方法可以供实现者实现的一种机制
代码如下:
//实现接口回调机制
private TopbarClickListener listener;
//接口
public interface TopbarClickListener{
public void ButtonClick();
}
//回调
public void SetOnTopbarClickListener(TopbarClickListener listener){
this.listener=listener;
}
这样,我们的自定义控件就实现了
7、使用自定义控件
其实使用自定义控件是和使用系统自带控件是一样的,有两点需要注意:
第一是命名控件,在res/包名
第二是我们的控件在布局文件的定义:要导入类
如下:
<com.example.topbar.TopBarView
android:id="@+id/topbar"
android:layout_height="45dp"
android:layout_width="match_parent"
myview:Title="标题"
myview:TitleColor="#000000"
myview:TitleSize="20sp"
myview:ButtonTitle="按钮"
myview:ButtonColor="#000000"
myview:ButtonSize="20sp"
/>
命名空间:
xmlns:myview="http://schemas.android.com/apk/res/com.example.topbar"
这样我们的布局写好了
最后是在主界面调用我们的自定义View
package com.example.topbar;
import com.example.topbar.TopBarView.TopbarClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
private TopBarView topbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topbar=(TopBarView) findViewById(R.id.topbar);
topbar.SetOnTopbarClickListener(new TopbarClickListener() {
@Override
public void ButtonClick() {
Toast.makeText(MainActivity.this, "按钮被点击", Toast.LENGTH_LONG).show();
}
});
}
自定义控件的View代码如下:
package com.example.topbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TopBarView extends RelativeLayout {
//1、自定义控件初始化
private Button button;
private TextView title;
//2、自定义属性的初始化
//title的属性
private String Title;
private int TitleColor;
private float TitleSize;
//Button的属性
private String ButtonTitle;
private int ButtonColor;
private float ButtonSize;
//在控件的属性都设置好后,设置包裹内容,位于父组件的位置的相关属性
private LayoutParams TitleParams;
private LayoutParams ButtonParams;
//实现接口回调机制
private TopbarClickListener listener;
//接口
public interface TopbarClickListener{
public void ButtonClick();
}
//回调
public void SetOnTopbarClickListener(TopbarClickListener listener){
this.listener=listener;
}
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
//在构造方法中完成初始化View对象
//1、将定义attrs的属性添加到TypedArrray中
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
//2、将存储在TypedArray中的属性添加到并回收TypedArray
Title=ta.getString(R.styleable.TopBar_Title);
TitleColor=ta.getColor(R.styleable.TopBar_TitleColor, 0);
TitleSize=ta.getFloat(R.styleable.TopBar_TitleSize, 16);
ButtonTitle=ta.getString(R.styleable.TopBar_ButtonTitle);
ButtonColor=ta.getColor(R.styleable.TopBar_ButtonColor, 0);
ButtonSize=ta.getFloat(R.styleable.TopBar_ButtonSize, 14);
ta.recycle();
//3、将各自的属性增加到各自的空间中
button=new Button(context);
title=new TextView(context);
title.setText(Title);
title.setTextColor(TitleColor);
title.setTextSize(TitleSize);
title.setGravity(Gravity.CENTER);
button.setText(ButtonTitle);
button.setTextColor(ButtonColor);
button.setTextSize(ButtonSize);
//到这里已经完成了自定义控件的各个组成成分
setBackgroundColor(0xFF00FFDA);
//接下来实现控件的组合
//首先使用 LayoutParams进行剩余属性的设置
TitleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(title, TitleParams);
ButtonParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
ButtonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(button, ButtonParams);
}
}