标签:自定义view属性
先在values下定义一个attrs的xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Topbar"> <attr name="title" format="string"/> <attr name="titleTextSize" format="dimension"/> <attr name="titleTextColor" format="color"/> <attr name="leftText" format="string"/> <attr name="leftTextColor" format="color"/> <attr name="leftBackground" format="color|reference"/> <attr name="rightText" format="string"/> <attr name="rightTextColor" format="color"/> <attr name="rightBackground" format="color|reference"/> </declare-styleable> </resources>定义一个declare-styleable format指定格式
2. Topbar extends RelativeLayout类
public class Topbar extends RelativeLayout{ private Button leftButton=null,rightButton=null; private TextView tvTitle=null; private int leftTextColor; private Drawable leftBackground; private String leftText=null; private int rightTextColor; private Drawable rightBackground; private String rightText=null; private float titleTextSize; private int titleTextColor; private String title; private topbarClickListener listener; private LayoutParams leftParams,rightParams,titleParams; private void init_attrs(TypedArray typedArray) { leftTextColor=typedArray.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackground=typedArray.getDrawable(R.styleable.Topbar_leftBackground); leftText=typedArray.getString(R.styleable.Topbar_leftText); rightTextColor=typedArray.getColor(R.styleable.Topbar_rightTextColor, 0); rightBackground=typedArray.getDrawable(R.styleable.Topbar_rightBackground); rightText=typedArray.getString(R.styleable.Topbar_rightText); titleTextSize=typedArray.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor=typedArray.getColor(R.styleable.Topbar_titleTextColor, 0); title=typedArray.getString(R.styleable.Topbar_title); } public Topbar(Context context) { super(context); } public Topbar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //自定义复写此构造函数 public Topbar(Context context, AttributeSet attrs) { super(context, attrs); //TypedArray获取styleable中定义的属性 TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.Topbar); //初始化属性 init_attrs(typedArray); //刷新 typedArray.recycle(); //初始化控件 init_views(context); //加入父控件 addToParentView(); } private void init_views(Context context) { leftButton=new Button(context); rightButton=new Button(context); tvTitle=new TextView(context); leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackground); leftButton.setText(leftText); leftButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { listener.leftClickListener(); } }); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); rightButton.setText(rightText); rightButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //实际的是在点击事件中调用,回调的方法 listener.rightClickListener(); } }); tvTitle.setTextColor(titleTextColor); tvTitle.setTextSize(titleTextSize); tvTitle.setText(title); tvTitle.setGravity(Gravity.CENTER); setBackgroundColor(0xCCCCCCCC); } private void addToParentView() { leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); addView(leftButton, leftParams); rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); addView(rightButton, rightParams); titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(tvTitle, titleParams); } //回调方法 public void setOnTopbarClickListener(topbarClickListener topbarClickListener) { this.listener=topbarClickListener; } //自定义回调接口 public interface topbarClickListener { public void leftClickListener(); public void rightClickListener(); } }代码看着很长,但实际很多都是重复滴。
使用:
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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.example.customviewdemo"> <com.example.customviewdemo.Topbar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp" app:leftText="返回" app:leftTextColor="#fff59563" app:leftBackground="@drawable/ic_launcher" app:rightText="下一页" app:rightTextColor="#fff59563" app:title="导航栏" app:titleTextSize="20.0px" app:titleTextColor="#fff59563" /> </RelativeLayout>xmlns:app定义一个新的命名空间,才能使用app:title等自定义的属性。
mainActivity中
bar=(Topbar) this.findViewById(R.id.topbar); bar.setOnTopbarClickListener(new Topbar.topbarClickListener() { @Override public void rightClickListener() { Toast.makeText(MainActivity.this, "hello right button", Toast.LENGTH_LONG).show(); } @Override public void leftClickListener() { Toast.makeText(MainActivity.this, "hello left button", Toast.LENGTH_LONG).show(); } });
谈一下报错,本人是先学习iOS,最近敲一些Android的代码,很多细节都容易出错。
错误提示:The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
错误原由:在makeText的第一个参数Context指的是上下文对象,而此处上下文并不是该Activity。。。你必须使用自己的Activity.this如我的是Toast.makeText(MainActivity.this, "已经到了第一周", Toast.LENGTH_SHORT).show() ;
标签:自定义view属性
原文地址:http://blog.csdn.net/nothingl3/article/details/45061317