1.com_title.xml
<?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="60dp">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="检测"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/whrite"
android:background="@color/colorPrimary"/>
<LinearLayout
android:id="@+id/linear_back"
android:layout_width="60dp"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/jiantoubk"/>
</LinearLayout>
</RelativeLayout>
2.ComTitle.java
public class ComTitle extends RelativeLayout {
private TextView textView;
private LinearLayout linearLayout;
public ComTitle(Context context) {
this(context,null);
}
public ComTitle(Context context, AttributeSet attrs) {
this(context, attrs,0);
initView(context,attrs);
}
public ComTitle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initView(Context context,AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.com_title,this);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textView = view.findViewById(R.id.text_title);
linearLayout = view.findViewById(R.id.linear_back);
linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = (Activity) context;
activity.finish();
}
});
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ComTitle);
String title = typedArray.getString(R.styleable.ComTitle_title);
int linearBk = typedArray.getInt(R.styleable.ComTitle_visibility,1);//默认value值
if (linearBk == 0){
linearLayout.setVisibility(VISIBLE);
}else {
linearLayout.setVisibility(GONE);
}
textView.setText(title);
}
}
3.在value文件夹下创建attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ComTitle">
<attr name="title" format="string"/>
<attr name="visibility" format="enum">
<enum name="visible" value="0"/>
<enum name="gone" value="1"/>
</attr>
</declare-styleable>
</resources>
4.在layout中引用
<com.gsunis.lanepad.views.ComTitle
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:visibility="visible"
app:title="流水详情"></com.gsunis.lanepad.views.ComTitle>