android自定义View分为下列几种形式:
一、系统控件重新组合,实现特定功能
这一类又可以分为两类:
1、要求复用
这一类首先就是要把一些这组合需要用到的控件,将其封装到一个布局xml布局文件中。:
例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="68dip"
android:id="@+id/aaa"
>
<TextView
android:id="@+id/tv_update_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="是否升级" />
<TextView
android:layout_below="@id/tv_update_title"
android:id="@+id/tv_update_content"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="停止更新" />
<CheckBox
android:checked="false"
android:id="@+id/cb_isupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
然后是自定义View:
package com.frank.mobilesafe.ui;
import com.frank.mobilesafe.R;
import android.R.bool;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
//此处继承的是RelativeLayout!!
public class SettingItemView extends RelativeLayout {
private CheckBox cb_update;
private TextView tv_update_title;
private TextView tv_update_content;
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
private void initView(Context context) {
// TODO Auto-generated method stub
View.inflate(context, R.layout.setting_item_view, this);
cb_update = (CheckBox) findViewById(R.id.cb_isupdate);
tv_update_title = (TextView) findViewById(R.id.tv_update_title);
tv_update_content = (TextView) findViewById(R.id.tv_update_content);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
/**
* 检查是否选中
* @return
*/
public boolean isChecked() {
return cb_update.isChecked();
}
/**
* 设置组合控件的状态
* @param isChecked
*/
public void SetChecked(boolean isChecked) {
cb_update.setChecked(isChecked);
}
/**
* 设置描述信息
* @param isChecked
*/
public void SetDesc(String text) {
tv_update_content.setText(text);
}
}
接着是在主界面中引用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_maintitle"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#8866ff00"
android:gravity="center"
android:text="设置中心"
android:textSize="22sp" />
<com.frank.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
最后是在主界面调用:
public class SettingActivity extends Activity {
private SettingItemView siv_update;
private SharedPreferences sp_update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
siv_update = (SettingItemView) findViewById(R.id.siv_update);
sp_update = getSharedPreferences("config",MODE_PRIVATE);
boolean update = sp_update.getBoolean("update", false);
if (update) {
siv_update.SetChecked(true);
siv_update.SetDesc("有新版本则更新");
}
else
{
siv_update.SetChecked(false);
siv_update.SetDesc("停止更新");
}
siv_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = sp_update.edit();
// TODO Auto-generated method stub
if (siv_update.isChecked()) {
siv_update.SetChecked(false);
siv_update.SetDesc("停止更新");
editor.putBoolean("update", false);
}
else{
siv_update.SetChecked(true);
siv_update.SetDesc("有新版本则更新");
editor.putBoolean("update", true);
}
}
});
}
}
(上述整理自http://www.cnblogs.com/xuhongfei/p/4009881.html)
2、不要求复用:
这一类只需要在xml文件中将布局文件写好,然后直接在activity中完成其逻辑即可
接下来会讲: