使用com.example.nate.mytopbar.Topbar之后报错
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=" 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"> <com.example.nate.mytopbar.Topbar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp" app:LeftBackground="@drawable/blue_color" app:LeftText="Back" app:LeftTextColor="#FFFFFF" app:rightBackground="@drawable/blue_color" app:rightText="More" app:rightTextColor="#FFFFFF" app:title="自定义标题" app:titleColor="#123412" app:titleSize="10sp" > </com.example.nate.mytopbar.Topbar> </RelativeLayout>
package com.example.nate.mytopbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Topbar extends RelativeLayout {
private Button leftbutton;
private Button rightbutton;
private TextView tvtitle;
private int leftcolor;
private Drawable leftbackground;
private String lefttext;
private int rightcolor;
private Drawable rightbackground;
private String righttext;
private int titlecolor;
private float titlesize;
private String titletext;
private LayoutParams leftparams,rightparams,titleparams;
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
//拿到自己定义的属性
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.Topbar );
leftcolor=ta.getColor(R.styleable.Topbar_LeftTextColor,0);
leftbackground=ta.getDrawable(R.styleable.Topbar_LeftBackground);
lefttext=ta.getString(R.styleable.Topbar_LeftText);
rightcolor=ta.getColor(R.styleable.Topbar_rightTextColor,0);
rightbackground=ta.getDrawable(R.styleable.Topbar_rightBackground);
righttext=ta.getString(R.styleable.Topbar_rightText);
titlecolor=ta.getColor(R.styleable.Topbar_titleColor,0);
titlesize=ta.getDimension(R.styleable.Topbar_titleSize,0);
titletext=ta.getString(R.styleable.Topbar_title);
//回收 1.防止浪费资源 2.避免由于一些值的缓存发生错误
ta.recycle();
//创建控件
leftbutton=new Button(context);//context上下文
rightbutton=new Button(context);
tvtitle=new TextView(context);
//为三个控件设置属性
rightbutton.setTextColor(rightcolor);
rightbutton.setBackground(rightbackground);
rightbutton.setText(righttext);
leftbutton.setTextColor(leftcolor);
leftbutton.setBackground(leftbackground);
leftbutton.setText(lefttext);
tvtitle.setText(titletext);
tvtitle.setTextColor(titlecolor);
tvtitle.setTextSize(titlesize);
tvtitle.setGravity(Gravity.CENTER);
//设置整体布局的背景颜色
setBackgroundColor(0xFFF59563);
//为左布局参数,第一属性宽,第二属性高
leftparams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);//相对父类左对齐
//添加控件到viewgroup中
addView(leftbutton,leftparams);
rightparams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rightparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightbutton,rightparams);
titleparams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleparams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvtitle,titleparams);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="title" format="string"/>
<attr name="titleSize" format="dimension"/>
<attr name="titleColor" format="color"/>
<attr name="LeftText" format="string"/>
<attr name="LeftTextColor" format="color"/>
<attr name="LeftBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
错误如图:
万分感谢