定义继承RelativeLayout的组合控件

本文介绍如何创建一个自定义的TopBar组件,该组件继承自RelativeLayout,并用于替代顶部ActionBar。文章详细介绍了定义属性、实现控件及在Activity中使用的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义继承RelativeLayout的组合控件,该控件适用于替换顶部ActionBar

1、首先在values文件夹中定义属性文件attr.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="ttitle" format="string"/>//标题
<attr name="titleTextSize" format="dimension" />//标题大小
<attr name="titleTextColor" format="color" />//标题颜色
<attr name="leftTextColor" format="color" />//左边文字大小
<attr name="leftBackground" format="reference|color" />//左边背景
<attr name="leftText" format="string" /> //左边文字
<attr name="rightTextColor" format="color" /> //右边文字颜色
<attr name="rightBackground" format="reference|color" />//右边背景
<attr name="rightText" format="string" /> //右边文字
</declare-styleable>
</resources>

2、定义继承自RelativeLayout的TopBar类
代码如下:

 public class TopBar extends RelativeLayout {

    //所有组合的控件
    private Button  leftButton ,rightButton;
    private TextView tvTitle;

    //定义好所有控件所需的属性
    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private float titleTextSize;
    private int titleTextColor;
    private String title;
    //定义所有控件需要的布局规则LayoutParams相当于把定义好的子视图布局样式告诉父布局
    private LayoutParams leftParams,rightParams,titleParams;
    //自定义的顶部ActionBar的点击监听;
    private  topbarClickListener listener;
    //点击事件的监听接口
    public interface topbarClickListener {
        public void leftClick();
        public void rightClick();
    }
    //提供activity调用的方法,类似于Button类的setOnClickListener(OnClickListener listener)
    //传入具体实现方法
    public void setOnTopbarClickListener(topbarClickListener listener){
        this.listener=listener;
    }

    //重写构造函数,获取自定义的属性
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //通过context.obtainStyledAttributes读取自定义的atts.xml属性文件
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);
        //得到设置的控件属性值,用来设置给控件
        leftTextColor=ta.getColor(R.styleable.Topbar_leftTextColor,0);
        leftBackground=ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftText=ta.getString(R.styleable.Topbar_leftText);

        rightTextColor=ta.getColor(R.styleable.Topbar_rightTextColor,0);
        rightBackground=ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightText=ta.getString(R.styleable.Topbar_rightText);

        titleTextSize=ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
        titleTextColor=ta.getColor(R.styleable.Topbar_titleTextColor,0);
         title=ta.getString(R.styleable.Topbar_ttitle);

        //释放TypedArray
        ta.recycle();
        //实例化需要的控件
        leftButton = new Button(context);
        rightButton = new Button(context);
        tvTitle = new TextView(context);

        //设置控件属性
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
        tvTitle.setTextColor(titleTextColor);
        tvTitle.setText(title);
        tvTitle.setGravity(Gravity.CENTER);
        tvTitle.setSingleLine(true); 
        tvTitle.setFocusableInTouchMode(true);
        tvTitle.setEllipsize(TruncateAt.MARQUEE); 

        //定义左边TextView控件的hight,width
        leftParams=new LayoutParams((int) getResources().getDimension(R.dimen.daohansize),(int) getResources().getDimension(R.dimen.daohansize));
        //设置位于父布局的位置
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        //向ViewGroup中添加控件和位置
        addView(leftButton,leftParams);

        rightParams=new LayoutParams((int) getResources().getDimension(R.dimen.daohansize),(int) getResources().getDimension(R.dimen.daohansize));
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        rightParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        addView(rightButton,rightParams);

        titleParams=new LayoutParams((int) getResources().getDimension(R.dimen.titlewith),LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(tvTitle,titleParams);


        leftButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                listener.leftClick();
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                listener.rightClick();
            }
        });
    }
    public void setleftButtonVisib(boolean flag){
        if(flag){
            leftButton.setVisibility(View.VISIBLE);
        }else{
            leftButton.setVisibility(View.INVISIBLE);
        }
    }
    public void setrightButtonVisib(boolean flag){
        if(flag){
            rightButton.setVisibility(View.VISIBLE);
        }else{
            rightButton.setVisibility(View.INVISIBLE);
        }
    }

}

用法:要在父布局中加上命名空间 xmlns:custom=”http://schemas.android.com/apk/res/com.example.all”
其中custom是名字可以随便命名,将包名替换http://schemas.android.com/apk/res/android
中的android即可。
在控件中 可以申明自己定义的属性并赋值

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.all"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
<com.example.all.view.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        custom:rightBackground="@drawable/nadaohan"
        custom:titleTextColor="@color/red"
        custom:ttitle="@string/huodong"
        custom:leftBackground="@drawable/back"
        />
</LinearLayout>

在activity中的使用:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        TopBar topbar = (TopBar)findViewById(R.id.topbar);
        topbar.setOnTopbarClickListener(listener);
    }

private topbarClickListener listener = new topbarClickListener(){

        public void leftClick() {
            finish() ;
        }

        public void rightClick() {

        }

    };

到此就结束了整个流程,从定义属性-定义控件-应用控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值