代码下载 点击打开链接
首先看主布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:setting="http://schemas.android.com/apk/res/com.example.mycustomeitem" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <com.example.mycustomeitem.CustomeItem android:id="@+id/zhuti" android:layout_width="fill_parent" android:layout_height="@dimen/item_height" android:layout_marginTop="10dp" setting:Divider="@color/diverColor" setting:TopDivider="@color/diverColor" setting:content="@string/zhuti" setting:contentColor="@color/contentColor" setting:contentSize="@dimen/contentSize" setting:leftImg="@drawable/zhuti" setting:leftImgSize="@dimen/leftImgSize" setting:rightImg="@drawable/arrow_right" setting:rightImgSize="@dimen/rightImgSize" /> <com.example.mycustomeitem.CustomeItem android:id="@+id/contact" android:layout_width="fill_parent" android:layout_height="@dimen/item_height" setting:Divider="@color/diverColor" setting:content="@string/contact" setting:contentColor="@color/contentColor" setting:contentSize="@dimen/contentSize" setting:leftImg="@drawable/contact" setting:leftImgSize="@dimen/leftImgSize" setting:rightImg="@drawable/arrow_right" setting:rightImgSize="@dimen/rightImgSize" /> <com.example.mycustomeitem.CustomeItem android:id="@+id/setting" android:layout_width="fill_parent" android:layout_height="@dimen/item_height" setting:BottomDivider="@color/diverColor" setting:content="@string/setting" setting:contentColor="@color/contentColor" setting:contentSize="@dimen/contentSize" setting:leftImg="@drawable/shezhi" setting:leftImgSize="@dimen/leftImgSize" setting:rightImg="@drawable/arrow_right" setting:rightImgSize="@dimen/rightImgSize" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomeItem"> <attr name="leftImg" format="reference"></attr> <attr name="leftImgSize" format="dimension"></attr> <attr name="rightImg" format="reference"></attr> <attr name="rightImgSize" format="dimension"></attr> <attr name="content" format="string"></attr> <attr name="contentSize" format="dimension"></attr> <attr name="contentColor" format="color"></attr> <attr name="BottomDivider" format="color"></attr> <attr name="Divider" format="color"></attr> <attr name="TopDivider" format="color"></attr> </declare-styleable> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="contentColor">#000000</color> <color name="diverColor">#FFB6C1</color> <color name="pressed_color">#DA70D6</color> <color name="normal_color">#FFFFFF</color> </resources>
<resources> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="item_height">50dp</dimen> <dimen name="leftImgSize">20dp</dimen> <dimen name="rightImgSize">15dp</dimen> <dimen name="contentSize">16dp</dimen> <dimen name="commonMargin">10dp</dimen> <dimen name="dividerHeight">1dp</dimen> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyCustomeItem</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="zhuti">主题</string> <string name="contact">联系人</string> <string name="setting">设置</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/pressed_color"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/normal_color"/> </shape>
package com.example.mycustomeitem; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class CustomeItem extends RelativeLayout implements OnTouchListener { private static final int LEFT_IMG_FLAG = 0x123; private static final int CONTENT_FLAG = 0x234; private Context context; private int leftImg; private int rightImg; private int leftImgSize; private int rightImgSize; private int contentSize; private String content; private int contentColor; private int commonMargin; private int divider; private int topDivider; private int bottomDivider; private int dividerColor; private int dividerHeight; public interface ItemClick{ void onClick(); } private ItemClick itemClick; public void setItemClick(ItemClick itemClick) { this.itemClick = itemClick; } @SuppressLint("ResourceAsColor") @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: this.setBackgroundResource(R.drawable.shape_item); Log.i("TAG", "down"); break; case MotionEvent.ACTION_UP: this.setBackgroundResource(R.drawable.shape_item_normal); // this.setBackgroundResource(R.drawable.item_selector1); Log.i("TAG", "up"); if (itemClick !=null) { itemClick.onClick(); } break; default: break; } return true; } public CustomeItem(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initRes(attrs); addViews(); this.setOnTouchListener(this); } public CustomeItem(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public CustomeItem(Context context) { this(context, null); // TODO Auto-generated constructor stub } /** * <attr name="leftImg" format="reference"></attr> <attr name="rightImg" * format="reference"></attr> * * <attr name="leftImgSize" format="dimension"></attr> <attr * name="rightImgSize" format="dimension"></attr> <attr name="content" * format="string"></attr> <attr name="contentSize" * format="dimension"></attr> <attr name="contentColor" * format="color"></attr> * * @param attrs */ private void initRes(AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomeItem); leftImg = ta.getResourceId(R.styleable.CustomeItem_leftImg, -1); leftImgSize = ta.getDimensionPixelOffset( R.styleable.CustomeItem_leftImgSize, -1); rightImg = ta.getResourceId(R.styleable.CustomeItem_rightImg, -1); rightImgSize = ta.getDimensionPixelOffset( R.styleable.CustomeItem_rightImgSize, -1); contentSize = ta.getDimensionPixelOffset( R.styleable.CustomeItem_contentSize, -1); content = ta.getString(R.styleable.CustomeItem_content); contentColor = ta.getColor(R.styleable.CustomeItem_contentColor, -1); divider = ta.getColor(R.styleable.CustomeItem_Divider, -1); topDivider = ta.getColor(R.styleable.CustomeItem_TopDivider, -1); bottomDivider = ta.getColor(R.styleable.CustomeItem_BottomDivider, -1); ta.recycle(); commonMargin = (int) getResources().getDimension(R.dimen.commonMargin); dividerColor = getResources().getColor(R.color.diverColor); dividerHeight = (int) getResources() .getDimension(R.dimen.dividerHeight); } private void addViews() { if (divider != -1) { View divider = new View(context); divider.setBackgroundColor(dividerColor); LayoutParams dividerParams = new LayoutParams( LayoutParams.FILL_PARENT, dividerHeight); dividerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG); divider.setLayoutParams(dividerParams); addView(divider); } if (topDivider != -1) { View divider = new View(context); divider.setBackgroundColor(dividerColor); LayoutParams dividerParams = new LayoutParams( LayoutParams.FILL_PARENT, dividerHeight); dividerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); // dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG); divider.setLayoutParams(dividerParams); addView(divider); } if (bottomDivider != -1) { View divider = new View(context); divider.setBackgroundColor(dividerColor); LayoutParams dividerParams = new LayoutParams( LayoutParams.FILL_PARENT, dividerHeight); dividerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); // dividerParams.addRule(RelativeLayout.ALIGN_START, CONTENT_FLAG); divider.setLayoutParams(dividerParams); addView(divider); } // 左边 ImageView itemLeftImg = new ImageView(context); if (leftImg != -1) { itemLeftImg.setBackgroundResource(leftImg); } itemLeftImg.setId(LEFT_IMG_FLAG); RelativeLayout.LayoutParams itemLeftParams = new LayoutParams( leftImgSize, leftImgSize); itemLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); itemLeftParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); itemLeftParams.leftMargin = commonMargin; itemLeftImg.setLayoutParams(itemLeftParams); this.addView(itemLeftImg); // 文字 TextView itemContent = new TextView(context); itemContent.setText(content); itemContent.setTextColor(contentColor); itemContent.setId(CONTENT_FLAG); // int contentSizePx = PhoneUtils.dp2px(context, contentSize); // Log.i("TAG", "contentSizePx--"+contentSizePx); itemContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); LayoutParams contentParams = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); contentParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); contentParams.addRule(RelativeLayout.RIGHT_OF, LEFT_IMG_FLAG); contentParams.leftMargin = commonMargin; itemContent.setLayoutParams(contentParams); this.addView(itemContent); // rightImag ImageView itemRightImg = new ImageView(context); if (rightImg != -1) { itemRightImg.setBackgroundResource(rightImg); } RelativeLayout.LayoutParams itemRightParams = new LayoutParams( rightImgSize, rightImgSize); itemRightParams.rightMargin = commonMargin; itemRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); itemRightParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); itemRightImg.setLayoutParams(itemRightParams); this.addView(itemRightImg); } }
package com.example.mycustomeitem; import com.example.mycustomeitem.CustomeItem.ItemClick; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CustomeItem zhuti = (CustomeItem) findViewById(R.id.zhuti); zhuti.setItemClick(new ItemClick() { @Override public void onClick() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "点击了。。", 0).show(); } }); } }
package com.example.mycustomeitem; import android.content.Context; public class PhoneUtils { public static int dp2px(Context context,float dpValue){ float scale = context.getResources().getDisplayMetrics().density; return (int)(dpValue * scale +0.5f); } }