1、创建自定义的属性:
2、在自定义的布局中获取属性;
3、在mainActivity中使用 自定义控件,并使用自定义属性赋值。
values/attr.xml 文件;
<resources>
<declare-styleable name="Topbar">
<attr name="title" format="string"/>
<attr name="title_color" format="color"/>
<attr name="title_size" format="dimension"/>
<attr name="btn_height" format="dimension"/>
<attr name="btn_margin" format="dimension"/>
<attr name="btn_vertical_margin" format="dimension"/>
<attr name="btn_horizontal_margin" format="dimension"/>
<attr name="right_text" format="string"/>
<attr name="left_text" format="string"/>
<attr name="right_background" format="reference|color"/>
<attr name="left_background" format="reference|color"/>
declare-styleable>
resources>
package com.sjzs.customui.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.sjzs.customui.R;
/**
* Created by yy520 on 2018-3-24.
*/
public class TitleBar extends RelativeLayout{
private Button leftButton,rightButton;
private TextView titleText;
private LayoutParams leftParams,rightParams,titleParams;
private String title;
private int titleColor;
private float titleSize;
private int btnMargin,btn_v_margin,btn_h_margin;
private int btn_height;
private String rightText;
private String leftText;
private Drawable rightBackground;
private Drawable leftBackground;
public void setRightOnClickListener(OnClickListener onClickListener){
rightButton.setOnClickListener(onClickListener);
}
public void setLeftOnClickListener(OnClickListener onClickListener){
leftButton.setOnClickListener(onClickListener);
}
public TitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
title = ta.getString(R.styleable.Topbar_title);
titleColor = ta.getColor(R.styleable.Topbar_title_color,0);
titleSize = ta.getDimension(R.styleable.Topbar_title_size,0);
btnMargin = (int) ta.getDimension(R.styleable.Topbar_btn_margin,0);
btn_v_margin = (int) ta.getDimension(R.styleable.Topbar_btn_vertical_margin,0);
btn_h_margin = (int) ta.getDimension(R.styleable.Topbar_btn_horizontal_margin,0);
btn_height = (int) ta.getDimension(R.styleable.Topbar_btn_height,0);
rightText = ta.getString(R.styleable.Topbar_right_text);
leftText = ta.getString(R.styleable.Topbar_left_text);
rightBackground = ta.getDrawable(R.styleable.Topbar_right_background);
leftBackground = ta.getDrawable(R.styleable.Topbar_left_background);
btn_v_margin = (int) ta.getDimension(R.styleable.Topbar_btn_vertical_margin,0);
btn_h_margin = (int) ta.getDimension(R.styleable.Topbar_btn_horizontal_margin,0);
ta.recycle();//回收资源,防止内存泄漏
leftButton = new Button(context);
rightButton = new Button(context);
titleText = new TextView(context);
leftButton.setText(leftText);
leftButton.setTextColor(titleColor);
leftButton.setBackground(leftBackground);
leftButton.setPadding(10,0,10,0);
rightButton.setText(rightText);
rightButton.setTextColor(titleColor);
rightButton.setBackground(rightBackground);
rightButton.setPadding(10,0,10,0);
titleText.setText(title);
titleText.setTextColor(titleColor);
titleText.setTextSize(titleSize);
titleText.setGravity(Gravity.CENTER);
if(btn_v_margin == 0){
btn_v_margin = btnMargin;
}
if(btn_h_margin == 0){
btn_h_margin = btnMargin;
}
leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
leftParams.setMargins(btn_h_margin,btn_v_margin,0,btn_v_margin);
//设置button高度,Android 默认高度太高。
if(btn_height!=0)leftParams.height = btn_height;
addView(leftButton,leftParams);
rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
rightParams.setMargins(0,btn_v_margin,btn_h_margin,btn_v_margin);
if(btn_height!=0)rightParams.height = btn_height;
addView(rightButton,rightParams);
titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(titleText,titleParams);
}
}
public class MainActivity extends AppCompatActivity {
private Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
TitleBar view = (TitleBar) findViewById(R.id.title_bar);
view.setRightOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Right",Toast.LENGTH_LONG).show();
}
});
view.setLeftOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Left",Toast.LENGTH_LONG).show();
}
});
}
}
"1.0" encoding="utf-8"?>
.support.constraint.ConstraintLayout 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.sjzs.customui.MainActivity">
<com.sjzs.customui.ui.TitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGreen"
app:title = "标题"
app:title_color = "#FFFFFF"
app:title_size = "7dp"
app:right_text = "返回"
app:left_text = "菜单"
app:btn_height = "30dp"
app:btn_horizontal_margin = "15dp"
app:btn_vertical_margin = "10dp"
app:left_background = "@drawable/button_bg_frame"
app:right_background = "@drawable/button_bg_frame">
com.sjzs.customui.ui.TitleBar>
.support.constraint.ConstraintLayout>
其他属性:
color;
<resources>
<color name="colorGreen">#9CCB39color>
<color name="white">#FFFFFFcolor>
resources>
按钮背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/white"
/>
<solid android:color="@color/colorGreen">solid>
<corners android:radius="3dp" />
shape>