一.基础篇
java代码:
public class TestTitleView extends RelativeLayout {
private TextView back;
private TextView title;
public TestTitleView(Context context) {
super(context);
}
public TestTitleView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.test_text_title_view, this);
back= (TextView)findViewById(R.id.back_test_view);
title= (TextView)findViewById(R.id.title_test_view);
}
public TestTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setBackOnClickLister(OnClickListener lister){
back.setOnClickListener(lister);
}
public void setTitleText(String string){
title.setText(string);
}
}
layout代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/back_test_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="back"
android:textSize="15sp"
android:gravity="center"
android:textColor="@color/black"/>
<TextView
android:id="@+id/title_test_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="title"
android:textSize="15sp"
android:gravity="center"
android:textColor="@color/black"
android:layout_toRightOf="@id/back_test_view"
android:layout_marginRight="50dp"/>
</RelativeLayout>
注意:
LayoutInflater.from(context).inflate(R.layout.test_text_title_view, this)
这样查找,View.inflate()找不到。