先看主布局
activity_main.xml(就一个自定义控件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.customerview.MainActivity" > <com.example.customerview.CustomerView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
package com.example.customerview; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
CustomerView
package com.example.customerview; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class CustomerView extends LinearLayout implements View.OnClickListener { private Context context; public CustomerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public CustomerView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomerView(Context context) { super(context); init(context); } /** * 初始化 */ private void init(Context context) { this.context = context; inflate(context, R.layout.childview, this); View button1 = findViewById(R.id.button1); View button2 = findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Toast.makeText(context, "button1", 0).show(); break; case R.id.button2: Toast.makeText(context, "button2", 0).show(); break; default: break; } } }
<?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="horizontal" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
+++++++++++++++++++++++++++++++++++++++++++开始+++++++++++++++++++++++++++++++++++++++++++
activity_main.xml
<?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" > <com.aigestudio.customviewdemo.activities.CustomCheckBox android:id="@+id/main_ccb" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.aigestudio.customviewdemo.activities.CustomCheckBox> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/view_custom_check_box_on" android:layout_width="60dp" android:layout_gravity="center" android:scaleType="fitCenter" android:layout_height="60dp" /> <ImageView android:id="@+id/view_custom_check_box_off" android:layout_gravity="center" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="fitCenter" /> </FrameLayout>
package com.aigestudio.customviewdemo.activities; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.aigestudio.customviewdemo.R; import com.aigestudio.customviewdemo.activities.CustomCheckBox.CustomCheckBoxChangeListener; /** * 主界面 */ public class MainActivity extends Activity { private CustomCheckBox ccbTest; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ccbTest = (CustomCheckBox) findViewById(R.id.main_ccb); ccbTest.setCustomCheckBoxChangeListener(new CustomCheckBoxChangeListener() { @Override public void customCheckBoxOn() { Toast.makeText(MainActivity.this, "Check on", Toast.LENGTH_SHORT).show(); } @Override public void customCheckBoxOff() { Toast.makeText(MainActivity.this, "Check off", Toast.LENGTH_SHORT).show(); } }); } }MeasureUtil
package com.aigestudio.customviewdemo.activities; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Color; import android.util.DisplayMetrics; /** * 测绘工具类 */ public final class MeasureUtil { /** * 获取屏幕尺寸 * * @param activity * Activity * @return 屏幕尺寸像素值,下标为0的值为宽,下标为1的值为高 */ public static int[] getScreenSize(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); return new int[] { metrics.widthPixels, metrics.heightPixels }; } }
package com.aigestudio.customviewdemo.activities; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import com.aigestudio.customviewdemo.R; /** * 自定义CheckBox * */ public class CustomCheckBox extends FrameLayout { private ImageView ivCheckOn, ivCheckOff;// 两种状态的ImageView private boolean isCheck;// 是否被选中的标志值 public CustomCheckBox(Context context) { this(context, null); } public CustomCheckBox(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // 设置布局文件 LayoutInflater.from(context).inflate(R.layout.view_custom_check_box, this); // 获取控件元素 ivCheckOn = (ImageView) findViewById(R.id.view_custom_check_box_on); ivCheckOff = (ImageView) findViewById(R.id.view_custom_check_box_off); // 设置两个ImageView的点击事件 ivCheckOn.setOnClickListener(new ClickListener()); ivCheckOff.setOnClickListener(new ClickListener()); int imageOnResId = getResources().getIdentifier("bluedot", "drawable" , context.getPackageName()); int imageOffResId = getResources().getIdentifier("reddot", "drawable" , context.getPackageName()); // 设置显示资源 setOnImage(imageOnResId); setOffImage(imageOffResId); // 默认显示的是没被选中的状态 setCheckOff(); } /** * 设置开启状态时CustomCheckBox的图片 */ public void setOnImage(int resId) { ivCheckOn.setImageResource(resId); } /** * 设置关闭状态时CustomCheckBox的图片 */ public void setOffImage(int resId) { ivCheckOff.setImageResource(resId); } /** * 设置CustomCheckBox为关闭状态 */ public void setCheckOff() { isCheck = false; ivCheckOn.setVisibility(GONE); ivCheckOff.setVisibility(VISIBLE); } /** * 设置CustomCheckBox为开启状态 */ public void setCheckOn() { isCheck = true; ivCheckOn.setVisibility(VISIBLE); ivCheckOff.setVisibility(GONE); } /** * 状态改变监听接口 */ private CustomCheckBoxChangeListener customCheckBoxChangeListener;// 切换的监听器 public interface CustomCheckBoxChangeListener { void customCheckBoxOn(); void customCheckBoxOff(); } public void setCustomCheckBoxChangeListener( CustomCheckBoxChangeListener customCheckBoxChangeListener) { this.customCheckBoxChangeListener = customCheckBoxChangeListener; } /** * 自定义CustomCheckBox中控件的事件监听器 */ private class ClickListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.view_custom_check_box_on: setCheckOff(); customCheckBoxChangeListener.customCheckBoxOff(); break; case R.id.view_custom_check_box_off: setCheckOn(); customCheckBoxChangeListener.customCheckBoxOn(); break; } } } }