布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
<Button
android:id="@+id/left_img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/left_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/left_img"
android:padding="10dp"
android:textSize="20sp" />
<TextView
android:id="@+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:textSize="20sp" />
<TextView
android:id="@+id/right_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:textSize="20sp" />
</RelativeLayout>
topbar控件:
控件名:包名.类名
<com.skr.MyToaBar
android:id="@+id/my_topBar"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
app:left_background="@drawable/left_selector"
app:left_text="返回"
app:right_text="提交"
app:text_color="#fff"
app:title_text="首页"/>
创建topbar类
切记:一定在带有两个参数中操作
public class MyToaBar extends RelativeLayout {
private Button left_img;
private TextView left_text_view;
private TextView title_text_view;
private TextView right_text_view;
private OnTopBarClickListen topBarClickListen;
public MyToaBar(Context context) {
super(context);
}
//设置监听器
public void setMyTopBarOnClickListener(OnTopBarClickListen topBarOnClickListener){
this.topBarClickListen = topBarOnClickListener;
}
//定义接口
public interface OnTopBarClickListen{
void onLeftClick();
}
public MyToaBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.mytopbar_layout,this);
left_img = findViewById(R.id.left_img);
left_text_view = findViewById(R.id.left_text_view);
title_text_view = findViewById(R.id.title_text_view);
right_text_view = findViewById(R.id.right_text_view);
left_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
//获取自定义属性big赋值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopBar);
int left_background = typedArray.getResourceId(R.styleable.MyTopBar_left_background, 0);
String left_text = typedArray.getString(R.styleable.MyTopBar_left_text);
String title_text = typedArray.getString(R.styleable.MyTopBar_title_text);
String right_text = typedArray.getString(R.styleable.MyTopBar_right_text);
int color = typedArray.getColor(R.styleable.MyTopBar_text_color,0);
//释放资源
typedArray.recycle();
left_img.setBackgroundResource(left_background);
left_text_view.setText(left_text);
title_text_view.setText(title_text);
right_text_view.setText(right_text);
left_text_view.setTextColor(color);
title_text_view.setTextColor(color);
right_text_view.setTextColor(color);
}
public MyToaBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyToaBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
activity类
public class MainActivity extends AppCompatActivity {
private TopBar top_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
top_bar = findViewById(R.id.top_bar);
top_bar.setLeftAndRightOnClickListener(new TopBar.OnAttsClickLisener() {
@Override
public void onLeftClick() {
}
@Override
public void onRightClick() {
}
});
}
}