通用顶部自定义控件 可在XML自定义属性
来源:互联网 发布:js unselectable 编辑:程序博客网 时间:2024/05/27 19:25
直接上代码 自定义控件的主体内容
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() {@Overridepublic void onClick(View v) {if (topLayOutClickListener != null) {topLayOutClickListener.LeftBtnClick();}}});v_right.setOnClickListener(new OnClickListener() {@Overridepublic 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
<?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>
可以在XMl自定义属性中定义的项目 必须在 attr中定义
<!-- 通用 自定义控件 最顶部的提示栏 --> <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>
注意:在使用自定义的控件有时候会报 No resource identifier found for attribute ‘XXX’ in package 等错误
解决方法:命名空间后面的包名应该是AndroidManifest.xml文件中定义的package包名,而不是使用的这个自定义控件所处的包的包名。
0 0
- 通用顶部自定义控件 可在XML自定义属性
- 【Android自定义控件】布局悬停在顶部
- 自定义顶部TitleBar控件
- 在自定义控件中读取XML配置属性的值
- 如何在styles.xml中使用控件自定义属性
- 自定义属性(xml中可插入)
- 自定义控件——自定义控件属性(attr.xml,TypedArray)
- Android自定义控件并且使其可以在xml中自定义属性
- 自定义控件----自定义属性
- 自定义控件属性(attr.xml,TypedArray)
- Android--自定义控件的xml属性
- Android--自定义控件的xml属性
- android 自定义控件的XML属性
- Android 自定义控件xml设置属性
- 实现自定义顶部标题栏(自定义控件)
- Android自定义控件:如何使用自定义的XML属性
- Android自定义控件:如何使用自定义的XML属性
- android中自定义的控件,使用自定义属性attrs.xml
- iOS开发内存管理
- HDU3939 Sticks and Right Triangle 毕达哥拉斯三元组+容斥原理
- Merge k Sorted Lists 合并多个链表 归并 vector/copy
- 在myeclipse中用maven进行项目的打包时出现内存泄露问题
- 最常用的SAT数学题解答方法分享
- 通用顶部自定义控件 可在XML自定义属性
- YUVtoRGB RGB to YUV color convert formulas - ITU 601
- linux 命令之tcpdump
- Java Clob类型转String类型
- 如何使用Python 打开exel等各种文件或者应用,就用:os.startfile
- 第十六周项目1-平方根中的异常
- Audio Streaming mit UDP in C
- Eclipse 中设置JVM 内存及JVM监控调优
- android listview 异步加载图片并防止错位