直接上代码 自定义控件的主体内容
package com.x2l.onlineedu.mid.object; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.x2l.onlineedu.mid.R; public class TopLayout extends LinearLayout { private Context mContext; private ImageView v_left; private View v_main; private ImageView v_right; private TextView tv; TopLayOutClickListener topLayOutClickListener; public TopLayout(Context context) { super(context); mContext = context; } public TopLayout(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(attrs); } /** * 功 能:载入在Xml中设置的内容<br> * 时 间:2015年6月17日 下午2:21:07<br> * 注 意:<br> * * @param attrs */ private void init(AttributeSet attrs) { TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.TopLayout); int left_Src = a.getResourceId(R.styleable.TopLayout_leftImage_src, R.drawable.ic_app); int right_Src = a.getResourceId(R.styleable.TopLayout_rightImage_Visiable, R.drawable.ic_app); int left_Visiable = a.getInt(R.styleable.TopLayout_leftImage_Visiable, View.VISIBLE); int top_background = a.getResourceId(R.styleable.TopLayout_layout_background, R.drawable.top_main); String str = a.getString(R.styleable.TopLayout_text); int right_Visiable = a.getInt(R.styleable.TopLayout_rightImage_Visiable, View.VISIBLE); int text_Visiable = a.getInt(R.styleable.TopLayout_text_Visiable, View.VISIBLE); v_main = LayoutInflater.from(mContext).inflate(R.layout.toplayout, this, true); v_left = (ImageView) v_main.findViewById(R.id.top_layout_leftbtn); v_right = (ImageView) v_main.findViewById(R.id.top_layout_rightbtn); tv = (TextView) v_main.findViewById(R.id.top_layout_text); v_left.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (topLayOutClickListener != null) { topLayOutClickListener.LeftBtnClick(); } } }); v_right.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (topLayOutClickListener != null) { topLayOutClickListener.RightBtnClick(); } } }); v_main.setBackgroundResource(top_background); v_left.setImageResource(left_Src); v_left.setVisibility(left_Visiable); v_right.setImageResource(right_Src); v_right.setVisibility(right_Visiable); tv.setText(str); tv.setVisibility(text_Visiable); } @SuppressLint("NewApi") public TopLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; } public interface TopLayOutClickListener { void LeftBtnClick(); void RightBtnClick(); } public void setTopLayoutClickListener(TopLayOutClickListener clickListener) { this.topLayOutClickListener = clickListener; } }
<?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="match_parent" > <ImageView android:id="@+id/top_layout_leftbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="@dimen/top_text_margin_leftright" /> <TextView android:id="@+id/top_layout_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:layout_centerInParent="true" android:textSize="@dimen/top_text_size" /> <ImageView android:id="@+id/top_layout_rightbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="@dimen/top_text_margin_leftright" /> </RelativeLayout>
<!-- 通用 自定义控件 最顶部的提示栏 --> <declare-styleable name="TopLayout"> <attr name="leftImage_src" format="reference"></attr> <attr name="rightImage_src" format="reference"></attr> <attr name="layout_background" format="reference"></attr> <attr name="text" format="string"></attr> <attr name="leftImage_Visiable"> <enum name="visiable" value="0x00000000"></enum> <enum name="gone" value="0x00000008"></enum> <enum name="invisiable" value="0x00000004"></enum> </attr> <attr name="rightImage_Visiable"> <enum name="visiable" value="0x00000000"></enum> <enum name="gone" value="0x00000008"></enum> <enum name="invisiable" value="0x00000004"></enum> </attr> <attr name="text_Visiable"> <enum name="visiable" value="0x00000000"></enum> <enum name="gone" value="0x00000008"></enum> <enum name="invisiable" value="0x00000004"></enum> </attr> </declare-styleable>
使用上自定义控件的地方 testlayout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:topview="http://schemas.android.com/apk/res/com.x2l.onlineedu.mid" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.x2l.onlineedu.mid.object.TopLayout android:id="@+id/tixian_edit_password_topview" android:layout_width="match_parent" android:layout_height="@dimen/top_height" topview:layout_background="@drawable/top_main" topview:leftImage_src="@drawable/back" topview:rightImage_Visiable="gone" topview:text="提现" > </com.x2l.onlineedu.mid.object.TopLayout> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edit_background" android:hint="请输入账号登陆密码" /> <ImageView android:id="@+id/tixian_edit_password_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tixian_edit_password_btn" /> </LinearLayout>
解决方法:命名空间后面的包名应该是AndroidManifest.xml文件中定义的package包名,而不是使用的这个自定义控件所处的包的包名。
原文地址:http://blog.csdn.net/v587ge/article/details/46533663