自定义View

1.在attr.xml文件中设计需要的属性

<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="color"/>
    <attr name="rightTextColor" format="color"/>
    <attr name="rightBackground" format="reference|color"/>
</declare-styleable>


2.在类中实现View

a.

TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);

获取在布局中通过TypedArray储存对自定义View设置的值,

b.

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.getDimensionPixelSize(R.styleable.Topbar_titleTextSize,0);
titleTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0);
title = ta.getString(R.styleable.Topbar_title);
获取所有的属性值

c.

ta.recycle();
清空TypedArray所占用的资源,释放内存

d.

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.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER);//设置文字居中显示

setBackgroundColor(0xFFF59563);
对自定义View内的组合控件进行赋值,值就是b拿到的值

e.

leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);//leftButtonleftParams的方式放到布局中

rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rightParams);//rightButtonrightParams的方式放到布局中

titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle,titleParams);
让控件以设定的LayoutParams方式显示到布局中

3.在布局文件中应用自定义的View

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mytopbar.MainActivity">

    <com.example.mytopbar.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        custom:leftBackground="#FFCCCC"
        custom:leftTextColor="#000000"
        custom:leftText="Back"
        custom:rightBackground="#FFCCCC"
        custom:rightTextColor="#000000"
        custom:rightText="More"
        custom:title="自定义标题"
        custom:titleTextColor="#33CCFF"
        custom:titleTextSize="10sp"></com.example.mytopbar.TopBar>
重点是这个

xmlns:custom="http://schemas.android.com/apk/res-auto"
topbar引用这里引入的资源,就相当于java文件中的import
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值