通用顶部自定义控件 可在XML自定义属性 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    直接上代码 自定义控件的主体内容

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中设置的内容	 * 时  间:2015年6月17日 下午2:21:07	 * 注  意:	 * 	 * @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;	}}

    自定义控件相应的View toplayout.xml

<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"><imageview android:id="@+id/top_layout_leftbtn" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginleft="@dimen/top_text_margin_leftright" android:layout_width="wrap_content"><textview android:id="@+id/top_layout_text" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textcolor="@android:color/white" android:textsize="@dimen/top_text_size"><imageview android:id="@+id/top_layout_rightbtn" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginright="@dimen/top_text_margin_leftright" android:layout_width="wrap_content"></imageview></textview></imageview></relativelayout>

    可以在XMl自定义属性中定义的项目 必须在 attr中定义

<declare-styleable name="TopLayout"><enum name="visiable" value="0x00000000"></enum><enum name="gone" value="0x00000008"></enum><enum name="invisiable" value="0x00000004"></enum><enum name="visiable" value="0x00000000"></enum><enum name="gone" value="0x00000008"></enum><enum name="invisiable" value="0x00000004"></enum><enum name="visiable" value="0x00000000"></enum><enum name="gone" value="0x00000008"></enum><enum name="invisiable" value="0x00000004"></enum></declare-styleable>

    使用上自定义控件的地方 testlayout.xml

<li><com.x2l.onlineedu.mid.object.toplayout android:id="@+id/tixian_edit_password_topview" android:layout_height="@dimen/top_height" android:layout_width="match_parent" 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:background="@drawable/edit_background" android:hint="请输入账号登陆密码" android:layout_height="wrap_content" android:layout_width="wrap_content"><imageview android:id="@+id/tixian_edit_password_btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/tixian_edit_password_btn"></imageview></edittext></linearlayout>

    注意:在使用自定义的控件有时候会报 No resource identifier found for attribute ‘XXX’ in package 等错误

    解决方法:命名空间后面的包名应该是AndroidManifest.xml文件中定义的package包名,而不是使用的这个自定义控件所处的包的包名,

通用顶部自定义控件 可在XML自定义属性

电脑资料

通用顶部自定义控件 可在XML自定义属性》(https://www.unjs.com)。

最新文章