关于android自定义控件Topbar的学习

本文介绍了如何在Android应用中创建自定义TopBar视图,包括定义属性、创建继承自RelativeLayout的类、处理控件及布局,并实现动态控制。通过XML文件配置样式,使用TypeArray绑定属性。

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

【前言】
刚刚看完幕课网老师关于自定义VIEW topbar的课程,特此总结一下学习收获。

【开发环境】
Eclipse +Android Developer Tools+bluestack调试

**

一.在res/values下创建atts.xml

**

添加左右中三个控件的属性。

<resources>
    <declare-styleable name="TopBar">
        <attr name = "title" format = "string"/>
        <attr name = "titleTextSize" format = "dimension"/>
        <attr name = "titleTextColor" format = "color"/>
        <attr name = "leftText" format = "string"/>        
        <attr name = "leftTextColor" format = "color"/>
        <attr name = "leftBackground" format = "reference|color"/>
        <attr name = "rightText" format = "string"/>
        <attr name = "rightTextColor" format = "color"/>
        <attr name = "rightBackground" format = "reference|color"/>
    </declare-styleable>
</resources>

二.新建一个类继承自RelativeLayout

(1).声明属性。

    private int leftTextColor;
    private Drawable leftBackbound;
    private String leftText;
    //.....其余省略

(2)利用TypeArray的方法将attrs中的属性绑定。

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
        leftBackbound = ta.getDrawable(R.styleable.TopBar_leftBackground);
        leftText = ta.getString(R.styleable.TopBar_leftText);

(3)绑定完后调用方法ta.recycle();目的一是为了避免浪费资源,二是为了避免由于缓存引起的一些错误。

三.处理我们所需要的控件

        leftButton  = new Button(context);
        rightButton = new Button(context);
        title = new TextView(context);

        leftButton.setText(leftText);
        leftButton.setBackground(leftBackbound);
        leftButton.setTextColor(leftTextColor);

四.将控件放到layout里面

(1)声明LayoutParams变量

private LayoutParams leftParams,rightParams,titleParams;

(2) 添加控件及其规则

leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);
        ....//余下省略

五.引用自定义view

修改activity_main.xml中的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:cwh= "http://schemas.android.com/apk/res/com.example.topbar"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.topbar.MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.example.topbar.Topbar 
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        cwh:leftBackground = "@drawable/Square"
        cwh:leftText = "Back"
        cwh:leftTextColor = "#FFFFFF"
        cwh:rightBackground = "@drawable/Square"
        cwh:rightText = "More"
        cwh:rightTextColor = "#FFFFFF"
        cwh:title = "自定义标题"
        cwh:titleTextColor = "#123412"
        cwh:titleTextSize = "10sp">
        </com.example.topbar.Topbar>
 </RelativeLayout>

六.动态控制topbar

回调机制动态调用

private topbarClickListener listener;

    public interface topbarClickListener{
        public void leftClick();
        public void rightClick();
    }

    public void setOnTopbarClickListener(topbarClickListener listener){
        this.listener = listener;
    }

设置按钮的监听事件

leftButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.leftClick();
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.rightClick();
            }
        });

最后在MainAcitivity引用就可以动态控制了

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Topbar topbar = (Topbar) findViewById(R.id.topbar);
        topbar.setOnTopbarClickListener(new topbarClickListener() {

            @Override
            public void rightClick() {
                // TODO Auto-generated method stub

            }

            @Override
            public void leftClick() {
                // TODO Auto-generated method stub

            }
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值