Android 自定义titlebar控件(自定义UI控件)

1、创建自定义的属性:
2、在自定义的布局中获取属性;
3、在mainActivity中使用 自定义控件,并使用自定义属性赋值。

1、创建自定义的属性创建

values/attr.xml 文件;

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

2、在自定义的布局中获取属性;

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);
    }
}

3、在mainActivity布局中使用 自定义控件,并使用自定义属性赋值。

Activity:

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();
            }
        });
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<!-- xmlns:app="http://schemas.android.com/apk/res-auto" 自定义属性名称空间,没有它自定义属性会报错-->
<android.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>
</android.support.constraint.ConstraintLayout>

其他属性:
color;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorGreen">#9CCB39</color>
    <color name="white">#FFFFFF</color>
</resources>

按钮背景:

<?xml version="1.0" encoding="utf-8"?>
<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值