自定义标题栏(左中右三块)

xml version="1.0" encoding="utf-8"?>
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.test.custom.MainActivity">

            android:id="@+id/myTitleView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:centerText="自定义标题栏"
        app:centerTextColor="#ffffff"
        app:centerTextSize="20sp"
        app:center_BG_Color="@color/colorAccent"

        app:leftText="返回"
        app:leftTextColor="#000000"
        app:leftTextSize="20sp"
        app:left_BG_Color="@drawable/bg_color"

        app:rightText="确定"
        app:rightTextColor="#000000"
        app:rightTextSize="20sp"
        app:right_GB_Color="@drawable/bg_color">

    

//自定义View应用的布局

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:id="@+id/left_button"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center" />

            android:id="@+id/center_button"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:gravity="center" />

            android:id="@+id/right_button"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center" />



//attrs中自定义的属性

xml version="1.0" encoding="utf-8"?>

    name="TitleView">
        name="leftText" format="string">
        name="left_BG_Color" format="reference">//设置Drawable状态选择器
        name="leftTextSize" format="dimension">
        name="leftTextColor" format="color">

        name="centerText" format="string">
        name="center_BG_Color" format="color">
        name="centerTextSize" format="dimension">
        name="centerTextColor" format="color">

        name="rightText" format="string">
        name="right_GB_Color" format="reference">//设置Drawable状态选择器
        name="rightTextSize" format="dimension">
        name="rightTextColor" format="color">
    
public class MainActivity extends AppCompatActivity {

    private TitleView myTitleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTitleView = (TitleView) findViewById(R.id.myTitleView);
        myTitleView.setLeftClick(new TitleView.LeftClick() {
            @Override
            public void onClick(View v, String name) {
                Toast.makeText(MainActivity.this, name + "点击了", Toast.LENGTH_SHORT).show();
            }
        });

        myTitleView.setRighttClick(new TitleView.RighttClick() {
            @Override
            public void onClick(View v, String name) {
                Toast.makeText(MainActivity.this, name + "点击了", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
//自定义View页面

public class TitleView extends LinearLayout {

    private TextView left_button;
    private TextView center_button;
    private TextView right_button;

    public TitleView(Context context) {
        super(context);
        initView(context, null);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        //自定义view使用布局,这里必须是this,不然不显示
        View view = LayoutInflater.from(context).inflate(R.layout.mytitle, this);
        left_button = (TextView) view.findViewById(R.id.left_button);
        center_button = (TextView) view.findViewById(R.id.center_button);
        right_button = (TextView) view.findViewById(R.id.right_button);
        //查找属性,并设置
        initAttrs(attrs);

        left_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                leftClick.onClick(v, left_button.getText().toString());
            }
        });

        right_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rightClick.onClick(v, right_button.getText().toString());
            }
        });


    }
	自定义的接口,点击左右两个按钮弹出吐司,
    private LeftClick leftClick;

    public interface LeftClick {
        void onClick(View v, String name);
    }

    public void setLeftClick(LeftClick leftClick) {
        this.leftClick = leftClick;
    }

    private RighttClick rightClick;

    public interface RighttClick {
        void onClick(View v, String name);
    }

    public void setRighttClick(RighttClick rightClick) {
        this.rightClick = rightClick;
    }


    private void initAttrs(@Nullable AttributeSet attrs) {
        TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.TitleView);
        int leftTextColor = typedArray.getColor(R.styleable.TitleView_leftTextColor, Color.RED);
        float leftTextSize = typedArray.getDimension(R.styleable.TitleView_leftTextSize, 10);
        String leftText = typedArray.getString(R.styleable.TitleView_leftText);
        Drawable left_BG_Color = typedArray.getDrawable(R.styleable.TitleView_left_BG_Color);
        left_button.setTextColor(leftTextColor);
        left_button.setTextSize(leftTextSize);
        left_button.setText(leftText);
        left_button.setBackgroundDrawable(left_BG_Color);

        int centerTextColor = typedArray.getColor(R.styleable.TitleView_centerTextColor, Color.RED);
        float centerTextSize = typedArray.getDimension(R.styleable.TitleView_centerTextSize, 10);
        String centerText = typedArray.getString(R.styleable.TitleView_centerText);
        int center_BG_Color = typedArray.getColor(R.styleable.TitleView_center_BG_Color, Color.RED);
        center_button.setTextColor(centerTextColor);
        center_button.setTextSize(centerTextSize);
        center_button.setText(centerText);
        center_button.setBackgroundColor(center_BG_Color);


        int rightTextColor = typedArray.getColor(R.styleable.TitleView_rightTextColor, Color.RED);
        float rightTextSize = typedArray.getDimension(R.styleable.TitleView_rightTextSize, 10);
        String rightText = typedArray.getString(R.styleable.TitleView_rightText);
        Drawable right_GB_Color = typedArray.getDrawable(R.styleable.TitleView_right_GB_Color);
        right_button.setTextColor(rightTextColor);
        right_button.setTextSize(rightTextSize);
        right_button.setText(rightText);
        right_button.setBackgroundDrawable(right_GB_Color);
    }
}

你可能感兴趣的:(自定义标题栏(左中右三块))