课程链接:http://idcbgp.cn/learn/247
实现自定义Topbar可以大略分为如下三个步骤:
步骤1:定义atts.xml属性文件
<?xml version="1.0" encoding="utf-8"?>
<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="leftBackground" format="referencecolor"/>
<attr name="leftTextColor" format="color"/>
<attr name="rightText" format="string"/>
<attr name="rightBackground" format="referencecolor"/>
<attr name="rightTextColor" format="color"/>
</declare-styleable>
</resources>
步骤2:继承RelativeLayout,实现Topbar
public class Topbar extends RelativeLayout{
private Button leftButton,rightButton;
private TextView tvTitle;
// 左边button的属性
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
// 右边button的属性
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
// Title属性
private float titleTextSize;
private int titleTextColor;
private String title;
private LayoutParams leftParams,rightParams,titleParams; // 布局参数对象
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
// 获取自定义的属性
TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.Topbar);
// 实例化属性成员
titleTextSize=ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor=ta.getColor(R.styleable.Topbar_titleTextColor, 0);
title=ta.getString(R.styleable.Topbar_title);
// 省略实例化其他属性成员的代码...
ta.recycle(); // 回收数据,避免资源浪费
// 根据context实例化控件
tvTitle=new TextView(context);
leftButton=new Button(context);
rightButton=new Button(context);
// 为属性成员赋值
tvTitle.setTextSize(titleTextSize);
tvTitle.setTextColor(titleTextColor);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER); // 设置TextView文字居中显示
// 省略其他属性成员的赋值代码...
setBackgroundColor(0xFFF59563); // 给当前的Topbar设置背景颜色
// 实例化布局参数对象,另两个布局参数对象的实例化类似,这里省略
titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
// 为控件添加布局属性,省略另两个控件的类似操作
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
// 将控件添加到Topbar并传入对应的布局参数对象
addView(tvTitle, titleParams);
addView(leftButton, leftParams);
addView(rightButton, rightParams);
}
}
步骤3:在布局文件使用自定义的Topbar
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent">
<com.imooc.widget.Topbar
android:layout_width="match_parent"
android:layout_height="50dp"
<!-- 使用自定义属性 -->
custom:leftBackground="@drawable/button_selector"
custom:leftText="BACK"
custom:leftTextColor="@color/white"
custom:rightBackground="@drawable/button_selector"
custom:rightText="MORE"
custom:rightTextColor="@color/white"
custom:title="自定义标题"
custom:titleTextColor="#123412"
custom:titleTextSize="15sp">
</com.imooc.widget.Topbar>
</RelativeLayout>
點(diǎn)擊查看更多內(nèi)容
7人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)