ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android 自定义View:TopBar,ndk开发流程

2022-01-23 13:03:15  阅读:184  来源: 互联网

标签:ndk mLeftBtn 自定义 TopBar private context public ta


    zhj:topBarBackground="#00ff00">
</com.zhuanghongji.customviewzhj.view.TopBar>

TopBar.java :


import com.zhuanghongji.customviewzhj.R;

public class TopBar extends RelativeLayout {

private Button mLeftBtn;
private TextView mTitleTv;
// 左Button属性
private int leftBtnTextColor;
private Drawable leftBtnBackground;
private String leftBtnText;
// title属性
private int titleTextColor;
private String titleText;
private float titleTextSize;
// topBar属性
private Drawable topBarBackground;
// 布局属性
private LayoutParams leftBtnParams, titleParams;
// 点击事件监听器接口 -- public
public interface TopBarClickListener {
    public void leftBtnClick();
}
private TopBarClickListener listener;
// 设置监听器
public void setOnTopBarClickListener(TopBarClickListener listener) {
    this.listener = listener;
}
// 供调用的方法
public void setLeftButtonIsVisible(boolean visible) {
    if (visible) {
        mLeftBtn.setVisibility(View.VISIBLE);
    } else {
        mLeftBtn.setVisibility(View.GONE);
    }
}
// 三个构造方法,均调用均调用三个参数的那个
public TopBar(Context context) {
    this(context, null);
}
public TopBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
    // 取自定义属性 -- leftBtn
    leftBtnText = ta.getString(R.styleable.TopBar![](https://www.icode9.com/i/ll/?i=img_convert/7297b262077e297599865bc35ea8dd49.png)

_leftBtnText);

    leftBtnTextColor = ta.getColor(R.styleable.TopBar_leftBtnTextColor, Color.WHITE);
    leftBtnBackground = ta.getDrawable(R.styleable.TopBar_leftBtnBackground);
    // 取自定义属性 -- title
    titleText = ta.getString(R.styleable.TopBar_titleText);
    titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, Color.WHITE);
    titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 12);
    // 取自定义属性 -- topBar
    topBarBackground = ta.getDrawable(R.styleable.TopBar_topBarBackground);
    //回收TypeArray(避免浪费资源,避免以为缓存导致的错误)
    ta.recycle();
    mLeftBtn = new Button(context);
    mTitleTv = new TextView(context);
    //设置自定义属性
    mLeftBtn.setText(leftBtnText);
    mLeftBtn.setTextColor(leftBtnTextColor);
    mLeftBtn.setBackground(leftBtnBackground);
    mTitleTv.setText(titleText);
    mTitleTv.setTextSize(titleTextSize);
    mTitleTv.setTextColor(titleTextColor);
    setBackground(topBarBackground);  // 可以通过此代码改变topBar背景色

// setBackgroundColor(0x00ff00); // 在这里我们直接设置成绿色

    // 设置布局 -- 左Button
    leftBtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    leftBtnParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
    leftBtnParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
    addView(mLeftBtn, leftBtnParams);
    // 设置布局 -- title
    titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
    addView(mTitleTv, titleParams);
    mLeftBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.leftBtnClick();    //左Button点击事件
        }
    });
}

}

attrs.xml :


<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="TopBar">
    <!--文字-->
    <attr name="leftBtnText" format="string"/>
    <attr name="titleText" format="string"/>
    <!--文字颜色-->
    <attr name="leftBtnTextColor" format="color"/>

es>

<declare-styleable name="TopBar">
    <!--文字-->
    <attr name="leftBtnText" format="string"/>
    <attr name="titleText" format="string"/>
    <!--文字颜色-->
    <attr name="leftBtnTextColor" format="color"/>

标签:ndk,mLeftBtn,自定义,TopBar,private,context,public,ta
来源: https://blog.csdn.net/m0_66155412/article/details/122650062

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有