//MainActivity中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ff.customtitle.MainActivity"
>
<com.ff.customtitle.MyTitleView
android:id="@+id/mytitleview"
android:layout_width="match_parent"
android:layout_height="80dp"
app:Layout_bg_Color="#ffff00"
app:Title_Image="@mipmap/goback"
app:Title_Text="自定义标题"
app:Title_TextColor="#ff0000"
app:Title_TextSize="25sp"
></com.ff.customtitle.MyTitleView>
</LinearLayout>
//在自定义中引用的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#f00">
<ImageView
android:id="@+id/Title_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/Title_ext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题" />
</RelativeLayout>
//下面是自定义View继承了LinearLayout
public class MyTitleView extends LinearLayout {
private ImageView img;
private TextView title_text;
private RelativeLayout layout;
private int bg_color;
private int title_textColor;
private String Text;
private float title_textSize;
private Drawable title_image;
public MyTitleView(Context context) {
super(context);
initView(context, null);
}
public MyTitleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
private void initView(Context context, @Nullable AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.mytitle, this);
img = (ImageView) view.findViewById(R.id.Title_icon);
title_text = (TextView) view.findViewById(R.id.Title_ext);
layout = (RelativeLayout) view.findViewById(R.id.layout);
if (attrs == null) {
return;
}
//得到attrs布局文件
initAttrs(context, attrs);
//把得到的atrrs属性设置到控件上
setViewContent();
// 创建点击事件
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myTitleViewClickListener.OnClick(v);
}
});
}
private OnMyTitleViewClickListener myTitleViewClickListener;
public interface OnMyTitleViewClickListener {
void OnClick(View view);
}
public void setOnMyTitleViewClickListener(OnMyTitleViewClickListener myTitleViewClickListener) {
this.myTitleViewClickListener = myTitleViewClickListener;
}
//把得到的atrrs属性设置到控件上
private void setViewContent() {
img.setImageDrawable(title_image);
layout.setBackgroundColor(bg_color);
title_text.setText(Text);
title_text.setTextColor(title_textColor);
title_text.setTextSize(title_textSize);
}
//得到attrs布局文件
private void initAttrs(Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTitleView);
bg_color = typedArray.getColor(R.styleable.MyTitleView_Layout_bg_Color, Color.RED);
title_textColor = typedArray.getColor(R.styleable.MyTitleView_Title_TextColor, Color.BLACK);
Text = typedArray.getString(R.styleable.MyTitleView_Title_Text);
title_textSize = typedArray.getDimension(R.styleable.MyTitleView_Title_TextSize, 15);
title_image = typedArray.getDrawable(R.styleable.MyTitleView_Title_Image);
}
}
//MainActivity中使用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTitleView viewById = (MyTitleView) findViewById(R.id.mytitleview);
viewById.setOnMyTitleViewClickListener(new MyTitleView.OnMyTitleViewClickListener() {
@Override
public void OnClick(View view) {
}
});
}
}
//values 文件夹下面自定义attrs设置自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTitleView">
<attr name="Layout_bg_Color" format="color"></attr>
<attr name="Title_TextColor" format="color"></attr>
<attr name="Title_Text" format="string"></attr>
<attr name="Title_TextSize" format="dimension"></attr>
<attr name="Title_Image" format="reference"></attr>
</declare-styleable>
</resources>