attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TobBar">
<attr name="ltftBackGround" format="reference"/>
<attr name="rightBackGround" format="reference"/>
<attr name="titleText" format="string"/>
<attr name="titleSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
</declare-styleable>
</resources>
自定义标题的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/left"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
/>
<Button
android:id="@+id/right"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
</RelativeLayout>
自定义的类 以及单击事件接口回调
package com.bwie.space01.zidingyi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bwie.space01.R;
import java.util.ArrayList;
public class TopBar extends RelativeLayout
{
private final Button left;
private final Button right;
private final TextView text;
OnLeftAndRightClickListene listene;
public void OnLeftAndRightClickListene(OnLeftAndRightClickListene listene) {
this.listene =listene;
}
//创建接口
public interface OnLeftAndRightClickListene{
void onLeftClick();
void onRightClick();
}
public void setLeft(boolean flag)
{
if(flag)
{
left.setVisibility(View.VISIBLE);
}else{
left.setVisibility(View.GONE);
}
}
public void setRight(boolean flag)
{
if(flag)
{
right.setVisibility(View.VISIBLE);
}else{
right.setVisibility(View.GONE);
}
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//找到xml
LayoutInflater.from(context).inflate(R.layout.bar,this);
left = findViewById(R.id.left);
right = findViewById(R.id.right);
text = findViewById(R.id.text);
left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listene!=null)
{
listene.onLeftClick();
}
}
});
right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listene!=null)
{
listene.onRightClick();
}
}
});
//获得自定义属性并赋值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TobBar);
//得到左边的按钮
int leftBackGround = typedArray.getResourceId(R.styleable.TobBar_ltftBackGround, 0);
//得到右边的按钮
int rightBackGround = typedArray.getResourceId(R.styleable.TobBar_rightBackGround, 0);
//得到zi
String string = typedArray.getString(R.styleable.TobBar_titleText);
float textSize = typedArray.getDimension(R.styleable.TobBar_titleSize, 0);
int color = typedArray.getColor(R.styleable.TobBar_titleTextColor, 0x38ad5a);
//一定要释放资源
typedArray.recycle();
left.setBackgroundResource(leftBackGround);
right.setBackgroundResource(rightBackGround);
text.setText(string);
text.setTextSize(textSize);
text.setTextColor(color);
}
}
引用自定义view的引用
package com.bwie.space01.show;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Toast;
import com.bwie.space01.R;
import com.bwie.space01.zidingyi.TopBar;
public class ShouYe extends Fragment
{
private TopBar topBar;
private RecyclerView rec;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shouye, container, false);
topBar = view.findViewById(R.id.topBar);
//找到rec控件
rec = view.findViewById(R.id.rec);
topBar.OnLeftAndRightClickListene(new TopBar.OnLeftAndRightClickListene() {
@Override
public void onLeftClick() {
Toast.makeText(getActivity(),"点击了左边",Toast.LENGTH_SHORT).show();
}
@Override
public void onRightClick() {
Toast.makeText(getActivity(),"点击了右边",Toast.LENGTH_SHORT).show();
}
});
return view;
}
}