今晚是幾分焦慮的學習呀,想着第一次面試在週六,想多學點知識,但是發現好像其實自己的基礎都學完了,在找多點進階的學,離題了,恩,好的,入正題,如何在安卓中使用自定義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);
}
}